Jump to content

Problems with keys not being detected

Recommended Posts

I'm using this function to detect whether a key is being hold:

var keyDown=new Array(false,false,false,false,false,false); function keys(e,isDown) {keynum=-1;if (e.which) {  keynum=e.which;}else if (window.event) {  keynum=e.keyCode;}if (isDown && keynum==32) alert("Spacebar!"); // Test 1if (keynum==38) { // Arrow-up  keyDown[0]=isDown;}else if (keynum==39) { // Arrow-right  keyDown[1]=isDown;}else if (keynum==40) { // Arrow-down  keyDown[2]=isDown;}else if (keynum==37) { // Arrow-left  keyDown[3]=isDown;}else if (keynum==32) { // Spacebar  keyDown[4]=isDown;  if (isDown) alert("Spacebar!"); // Test 2}else if (keynum==80) { // P-key  keyDown[5]=isDown;}}

The function is called in the body-ellement:

<body onkeydown="keys(event,true)" onkeyup="keys(event,false)"><!-- Body contents... --></body>

Like you can see, when a button is pressed (or released) this is stored in the array. Thismakes it possible for functions to see if a key is being hold at any time (yes, maybe there are other ways to do this). But this combinations do not work (push the keys in the order I give them):1) Press the right and down-arrow;2) Press the spacebar; On my computer the spacebar is not detected, when trying this combination. This combination doesn't work either:1) Press the left and up-arrow;2) Press the spacebar; I really want the spacebar to work! Even the alert boxes do not show up when trying this.I tried this in Firefox en Chrome. Both browsers do not detect the spacebar when pressed. Is there something wrong with the script, or is it a javascript bug? Thanks in advance!

Edited by ReGA
Link to post
Share on other sites

I've had experience with three keys being pressed at the same time and not receiving all the signals. I don't know if this is on all keyboards or just laptop keyboards but there's nothing at the Javascript level (or ActionScript as I've experienced in the past) that can be done to fix it. It's possibly a hardware limitation but I haven't really researched beyond my own experience.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...