View Full Version : any body know how to capture system keypressing
kwhubby
11-06-2002, 06:33 AM
does anybody know how to trigger something when a system key is pressed (eg arrows, f buttons, shift, Ctrl, Alt etc).
it would greatly help for a javascript game i am making that right now requires use of specific letters instead of arrows to control the charater:)
glenngv
11-06-2002, 07:16 AM
use onkeydown instead of onkeypress then in your function, do something like this:
function checkKey(e)
{
var code = (document.all) ? event.keyCode:e.which;
var ctrl = (document.all) ? event.ctrlKey:e.modifiers & Event.CONTROL_MASK;
then you can detect if, say, CTRL+N is pressed
if (ctrl && code==78) {...}
for alt and shift keys, i'm guessing this is the way to detect. Not sure though, just an educated guess :D
for IE:
event.altKey
event.shiftKey
for NS:
Event.ALT_MASK
Event.SHIFT_MASK
for arrow keys and function keys, just alert the code variable to see the corresponding codes
you can play around with it to get what you exactly needed.
kwhubby
11-07-2002, 03:51 AM
thanks for your quick reply, It helps, I will do what you said to and figure out what the arrow keys are.
realisis
11-07-2002, 04:19 AM
browsers other than NS4 do assign the control-key to #17. I've used the following with success (IE treated separately as per glenn's example):
NS4 = document.layers
if ((NS4 && (evt.modifiers == Event.CONTROL_MASK)) || (!NS4 && (evt.which == 17)))
{ etc... }
Glenn is right about ALT_MASK and SHIFT_MASK... But Glenn I'm not sure if the & after e.modifiers was a typo? Even with &&, your formulation would check for recognition of e.modifiers, but not for presence of e.modifiers (see my formulation). Or am I missing something?
There's also something called META_MASK - anyone know what that is? Is that a MAC only thing or something else?
...
btw, kwhubby, though NS4 is supposed to have numbers assigned to the arrow keys, I've never been able to capture them. Anyone have the solution for these keys on NS4? I'm on Win 98SE, so this may be another one of NS4's bugs which is limited to only that platform.
edit: there's always a typo in there somewhere...
glenngv
11-07-2002, 05:16 AM
That is not a typo. That's a bitwise (not logical) AND operator.
http://www.webreference.com/js/column11/modifierkeys.html
kwhubby
11-07-2002, 06:37 AM
I figured out that the arrow keys are the following key codes
up: 38
right: 39
left: 37
down: 40
I did not think that all of the keys on the keyboard would trigger on keydown, nor did I think that they would have a a key value
I used this little script, it worked in ie (6) but strangely it did not display an alert in netscape (6). Does anybody know why it stoped at event.keyCode ??
<html>
<script>
function checkKey() {
var code = event.keyCode
alert(code)
}
</script>
<body>
<form>
<input onkeydown="checkKey()">
</form>
</body>
glenngv
11-07-2002, 06:50 AM
<html>
<script>
function checkKey(e) {
var code = (event)?event.keyCode:e.which;
alert(code)
}
</script>
<body>
<form>
<input onkeydown="checkKey(event)">
</form>
</body>
realisis
11-08-2002, 04:07 AM
Glenn:
"That's a bitwise (not logical) AND operator. "
Ah, I'd only used the bitwise OR so far...
Thanks for the tip and the link. Btw, that page also mentions the META-MASK without revealing which key it is... Search goes on...
kwhubby
11-08-2002, 05:38 AM
whats the difference between bitwise and logical?
glenngv
11-08-2002, 05:50 AM
http://developer.netscape.com/docs/manuals/communicator/jsref/ops.htm
kwhubby
11-08-2002, 06:23 AM
very interesting and helpful thanks
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.