3

The following script does what it should, that is, it reacts on the keys "arrow left" and "arrow right". However, due to a keycode clash, it reacts on a single quote as well. It makes it impossible to enter that character into an input field. Can anything be done about that?

<script type="text/javascript"> onload = function(){ document.onkeypress=function(e){ if(window.event) e=window.event; var keycode=(e.keyCode)?e.keyCode:e.which; switch(keycode){ case 37: window.location.href='set.jsp?index=5'; break; case 39: window.location.href='set.jsp?index=7'; break; } } } </script> 
4
  • For me ' is 222. I tested here: cambiaresearch.com/articles/15/javascript-char-codes-key-codes Commented Jan 9, 2015 at 13:34
  • Try keydown instead perhaps - stackoverflow.com/questions/20121252/… Commented Jan 9, 2015 at 13:34
  • @Halcyon For me, the test shows 222 as well. Still, this script reacts when a single quote is pressed, and an answer to stackoverflow.com/questions/12008215/… also uses the keycode 39 for a single quote. Commented Jan 9, 2015 at 13:43
  • keyCode and which are deprecated, you should use key. As others have said, the keypress event doesn't provide a key/keyCode/which value for keys that don't represent printable characters in all browsers, so use keypress or keyup and something like: var keycode = e.key || e.keyCode || e.which. Commented Jan 9, 2015 at 13:49

4 Answers 4

3

When the user presses the single quote key, the e.keyCode property is zero, and the e.which property is 39. Executing String.fromCharCode(39) returns a single quote.

You want the keyCode if that property is in the event object:

var keycode = "keyCode" in e ? e.keyCode : e.which; 

That way you get zero for the keyCode when that property exists in the event object, and when the which property also exists.

document.onkeydown = function(event) { event = event || window.event; var keyCode = "keyCode" in event ? event.keyCode : event.which; switch (keyCode) { case 37: console.log("37 was pressed", event); break; case 39: console.log("39 was pressed", event); break; } }; 

Edit #1: Other commenters and answers are correct. I forgot you shouldn't be detecting control keys with keypress events. Changed to onkeydown.

Full HTML example that works cross browser:

<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>Key Codes Test</title> </head> <body> <script type="text/javascript"> document.onkeydown = function(event) { event = event || window.event; var keyCode = "keyCode" in event ? event.keyCode : event.which; switch (keyCode) { case 37: console.log("37 was pressed", event); break; case 39: console.log("39 was pressed", event); break; } }; </script> <input type="text" size="30"> </body> </html> 
Sign up to request clarification or add additional context in comments.

1 Comment

Note that keyCode is deprecated, as is which, key should be used where available. Also see Legacy keyboard events.
1

keypress should not capture control keys like left/right arrow. if you use keydown event, single quote keycode is 222 definitely no conflict

Comments

1

As it is a text input, it seems you'd also have a problem when someone is trying to use the arrow keys to move the cursor within the input. Thus, stopping event propagation/bubbling should be used, and can solve the main issue you're asking about.

// assuming you've grabbed an input in var input_ele input_ele.onkeypress = function (e) { e = e || window.event; if (e.stopPropagation) { e.stopPropagation(); } else { e.cancelBubble = true; } }; 

Using this will stop the keypress event from leaving the input element, thereby never reaching the document element to trigger the unwanted behavior. In other words, you don't break the expected behavior of a very standard control element.

1 Comment

While it does not solve the main issue, it for sure has helped me to fix another problem in the app.
0

Use keydown instread of keypress

jS:

document.onkeydown=function(event){ if(window.event) event=window.event; var keycode=(event.keyCode)?event.keyCode:event.which; switch(keycode){ case 37: alert("an arrow"); break; case 39: alert("another arrow"); break; } } 

Fiddle : http://jsfiddle.net/p9x1Lj4u/2/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.