Home » Eclipse Projects » Remote Application Platform (RAP) » Key event handling in RAP
| | |
Re: Key event handling in RAP [message #509096 is a reply to message #508537] |
Thu, 21 January 2010 06:46 |
Niels Lippke Messages: 71 Registered: December 2009 |
Member |
|
|
This is a multi-part message in MIME format.
------=_NextPart_000_001E_01CA9A97.8CE50940
Content-Type: text/plain;
format=flowed;
charset="iso-8859-1";
reply-type=response
Content-Transfer-Encoding: 7bit
Hmm, before I get lost in the wood. Here are my current thoughts about the
key bindings.
Attached is a sample html page containing some js-functions I found in the
web. These functions allow the user to override the default browser
behaviour (e.g. CTRL-S which normally open the Save As Dialog in FF and
other browsers) with a custom function.
Here it overides CTRL-S to open an alert window.
So why not do similar in a RAP application and use those key-combinations
for global key bindings (e.g. org.eclipse.ui.bindings)?
Are there problems I don't quite grasp?
Regards,
Niels
------=_NextPart_000_001E_01CA9A97.8CE50940
Content-Type: text/html;
name="test.html"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="test.html"
<html>
<body> =20
<script type=3D"text/javascript">
/**
* http://www.openjs.com/scripts/events/keyboard_shortcuts/
* Version : 2.01.B
* By Binny V A
* License : BSD
*/
shortcut =3D {
'all_shortcuts':{},//All the shortcuts are stored in this array
'add': function(shortcut_combination,callback,opt) {
=09
var eventtype;
//Whether Browser is Gecko based
//alert(navigator.appName);
//alert(navigator.userAgent);
var ua =3D navigator.userAgent; =09
if (ua.indexOf("Safari") > 0) {
eventtype =3D 'keydown';
} else if (ua.indexOf("Opera") > 0) {
eventtype=3D'keypress'; =09
} else if (ua.indexOf("MSIE") > 0) {
eventtype=3D'keydown';
} else if (ua.indexOf("Firefox") > 0) {
eventtype=3D'keypress';
} else if (ua.indexOf("Chrome") > 0) {
eventtype=3D'keypress';
}
/*if (navigator.product =3D=3D 'Gecko') {
eventtype=3D'keypress'; =09
}
else {
eventtype =3D 'keydown';
}*/
//Provide a set of default options
var default_options =3D {
'type': eventtype,
'propagate': false,
'disable_in_input': false,
'target': document,
'keycode': false
}
=09
//Provide a set of default options
//var default_options =3D {
// 'type':'keydown',
// 'propagate':false,
// 'disable_in_input':false,
// 'target':document,
// 'keycode':false
//}
if(!opt) opt =3D default_options;
else {
for(var dfo in default_options) {
if(typeof opt[dfo] =3D=3D 'undefined') opt[dfo] =3D =
default_options[dfo];
}
}
var ele =3D opt.target;
if(typeof opt.target =3D=3D 'string') ele =3D =
document.getElementById(opt.target);
var ths =3D this;
shortcut_combination =3D shortcut_combination.toLowerCase();
//The function to be called at keypress
var func =3D function(e) {
e =3D e || window.event;
=09
if(opt['disable_in_input']) { //Don't enable shortcut keys in Input, =
Textarea fields
var element;
if(e.target) element=3De.target;
else if(e.srcElement) element=3De.srcElement;
if(element.nodeType=3D=3D3) element=3Delement.parentNode;
if(element.tagName =3D=3D 'INPUT' || element.tagName =3D=3D =
'TEXTAREA') return;
}
=09
//Find Which key is pressed
if (e.keyCode) code =3D e.keyCode;
else if (e.which) code =3D e.which;
var character =3D String.fromCharCode(code).toLowerCase();
=09
if(code =3D=3D 188) character=3D","; //If the user presses , when the =
type is onkeydown
if(code =3D=3D 190) character=3D"."; //If the user presses , when the =
type is onkeydown
var keys =3D shortcut_combination.split("+");
//Key Pressed - counts the number of valid keypresses - if it is same =
as the number of keys, the shortcut function is invoked
var kp =3D 0;
=09
//Work around for stupid Shift key bug created by using lowercase - =
as a result the shift+num combination was broken
var shift_nums =3D {
"`":"~",
"1":"!",
"2":"@",
"3":"#",
"4":"$",
"5":"%",
"6":"^",
"7":"&",
"8":"*",
"9":"(",
"0":")",
"-":"_",
"=3D":"+",
";":":",
"'":"\"",
",":"<",
".":">",
"/":"?",
"\\":"|"
}
//Special Keys - and their codes
var special_keys =3D {
'esc':27,
'escape':27,
'tab':9,
'space':32,
'return':13,
'enter':13,
'backspace':8,
=09
'scrolllock':145,
'scroll_lock':145,
'scroll':145,
'capslock':20,
'caps_lock':20,
'caps':20,
'numlock':144,
'num_lock':144,
'num':144,
=09
'pause':19,
'break':19,
=09
'insert':45,
'home':36,
'delete':46,
'end':35,
=09
'pageup':33,
'page_up':33,
'pu':33,
=09
'pagedown':34,
'page_down':34,
'pd':34,
=09
'left':37,
'up':38,
'right':39,
'down':40,
=09
'f1':112,
'f2':113,
'f3':114,
'f4':115,
'f5':116,
'f6':117,
'f7':118,
'f8':119,
'f9':120,
'f10':121,
'f11':122,
'f12':123
}
=09
var modifiers =3D {=20
shift: { wanted:false, pressed:false},
ctrl : { wanted:false, pressed:false},
alt : { wanted:false, pressed:false},
meta : { wanted:false, pressed:false} //Meta is Mac specific
};
=20
if(e.ctrlKey) modifiers.ctrl.pressed =3D true;
if(e.shiftKey) modifiers.shift.pressed =3D true;
if(e.altKey) modifiers.alt.pressed =3D true;
if(e.metaKey) modifiers.meta.pressed =3D true;
=20
for(var i=3D0; k=3Dkeys[i],i<keys.length; i++) {
//Modifiers
if(k =3D=3D 'ctrl' || k =3D=3D 'control') {
kp++;
modifiers.ctrl.wanted =3D true;
} else if(k =3D=3D 'shift') {
kp++;
modifiers.shift.wanted =3D true;
} else if(k =3D=3D 'alt') {
kp++;
modifiers.alt.wanted =3D true;
} else if(k =3D=3D 'meta') {
kp++;
modifiers.meta.wanted =3D true;
} else if(k.length > 1) { //If it is a special key
if(special_keys[k] =3D=3D code) kp++;
=09
} else if(opt['keycode']) {
if(opt['keycode'] =3D=3D code) kp++;
} else { //The special keys did not match
if(character =3D=3D k) kp++;
else {
if(shift_nums[character] && e.shiftKey) { //Stupid Shift key bug =
created by using lowercase
character =3D shift_nums[character];=20
if(character =3D=3D k) kp++;
}
}
}
}
=09
if(kp =3D=3D keys.length &&=20
modifiers.ctrl.pressed =3D=3D modifiers.ctrl.wanted &&
modifiers.shift.pressed =3D=3D modifiers.shift.wanted &&
modifiers.alt.pressed =3D=3D modifiers.alt.wanted &&
modifiers.meta.pressed =3D=3D modifiers.meta.wanted) {
callback(e);
=09
if(!opt['propagate']) { //Stop the event
//e.cancelBubble is supported by IE - this will kill the bubbling =
process.
e.cancelBubble =3D true;
e.returnValue =3D false;
e.preventDefault();
=09
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
return false;
}
}
}
this.all_shortcuts[shortcut_combination] =3D {
'callback':func,=20
'target':ele,=20
'event': opt['type']
};
//Attach the function with the event
if(ele.addEventListener) ele.addEventListener(opt['type'], func, =
false);
else if(ele.attachEvent) ele.attachEvent('on'+opt['type'], func);
else ele['on'+opt['type']] =3D func;
},
//Remove the shortcut - just specify the shortcut and I will remove the =
binding
'remove':function(shortcut_combination) {
shortcut_combination =3D shortcut_combination.toLowerCase();
var binding =3D this.all_shortcuts[shortcut_combination];
delete(this.all_shortcuts[shortcut_combination])
if(!binding) return;
var type =3D binding['event'];
var ele =3D binding['target'];
var callback =3D binding['callback'];
if(ele.detachEvent) ele.detachEvent('on'+type, callback);
else if(ele.removeEventListener) ele.removeEventListener(type, =
callback, false);
else ele['on'+type] =3D false;
}
}
</script>
<script type=3D"text/javascript">shortcut.add("Ctrl+S",function() {
alert("Hi there!");
});</script>
=09
=09
<!--<a href=3D"javascript:(function(){document.onkeypress =3D =
function(event){return event.preventDefault()}})()">Disable browser =
default behaviour!</a>-->
This page overrides the default browser behaviour for CTRL+S with a =
custom js-function.<br>
Sample Input field: <input type=3D"text">
=09
</body>
</html>
------=_NextPart_000_001E_01CA9A97.8CE50940--
|
|
| |
Goto Forum:
Current Time: Sun Nov 10 02:26:13 GMT 2024
Powered by FUDForum. Page generated in 0.03626 seconds
|