0

I want to fire a javascript function called 'faster' whenever the period key is pressed. I first tried a method which had worked for other keys:

document.onkeydown = function(evt) { 
  evt.preventDefault();
  evt = evt || window.event;
  switch (evt.keyCode) {
     case 190:
         faster();
          break;
   }
};

I also tried this with the code 46, but still no luck. One post said that Mac keyboards are less reliable and suggested using onkeypress instead of onkeydown. I couldn't find a directly relevant example, but I tried this:

JavaScript

periodetect () {
    var c = e.which || e.keyCode;
    if (c=46) {
       faster();
    }
};

HTML

<BODY BGCOLOR="#CCCCCC" onkeypress="periodetect()">

Can anyone spot my beginner mistake or suggest alternative?

Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
wkb
  • 5
  • 1

2 Answers2

3

This might be the solution you are looking for:
Requires jquery 1.9.x or later

$(document).bind('keydown',function(e){
    if(e.keyCode == 190) {
        alert("you pressed .");
    }
});

When you press the "period key" and alert will show, if you want it with a function just replace alert("you pressed ."); with faster();

Test it yourself: http://jsfiddle.net/d60jtqdc/

Velocity-plus
  • 663
  • 1
  • 5
  • 19
1

It turns out that keydown appears to be the right thing to use after all. It also turns out that keyCode is not capitalized in the OP's code given in the comment to my answer, but is correctly capitalized in the question. The examples below do work now that I've changed the first one.

Have a look at this: onKeyPress Vs. onKeyUp and onKeyDown

document.onkeydown = function(evt) { 
  evt.preventDefault();
  evt = evt || window.event;
  switch (evt.keyCode) {
     case 190:
         faster();
          break;
   }
};

Edited to add: Can someone test this code in a Mac, please?

<!DOCTYPE html>
<html>
<head>
<title>Stack Overflow Tester</title>
<style type="text/css">

</style>    
<script type="text/javascript">
//<![CDATA[
document.onkeydown = function(evt) { 
  evt.preventDefault();
  evt = evt || window.event;
  switch (evt.keyCode) {
     case 190:
         alert("Faster!");
          break;
   }
};
//]]>
</script>
</head>
<body>
<p>Stack Overflow Test; press the period.</p>
</body>
</html>
Community
  • 1
  • 1
Bob Brown
  • 1,463
  • 1
  • 12
  • 25
  • Thanks to all. I have tried all suggested methods (except for the jquery) and I still can't detect the period key. I suspect it may be something about the Macintosh keyboard. Instead of trying to guess what the keycode for period is on my machine, is there some way I can get a script to tell me what code it is receiving when I hit the key? – wkb Aug 24 '14 at 23:06
  • Just to be sure I understand, in your own first code example, you changed `onkeydown` to `onkeypress`, like above, and it doesn't work on a Mac? I couldn't make this work in a fiddle. I'm going to edit my answer to include complete code; can someone please check it on a Mac? – Bob Brown Aug 24 '14 at 23:17
  • Yes, I have tried both onkeydown and onkeypress with both 190 and 46 – wkb Aug 24 '14 at 23:21
  • ok, your test came up 190, as it should, but I still can't trap it. Here is my routine:document.onkeydown = function(evt) { evt.preventDefault(); evt = evt || window.event; // alert(evt.keyCode); switch (evt.keycode) { case 190: faster(); break; //alert(evt.keyCode); } }; The first alert shows 190, the second fails to fire. Could I have something illegal in some other routine that is messing with the interpreter? – wkb Aug 25 '14 at 00:37
  • Check capitalization of keyCode in your switch statement. – Bob Brown Aug 25 '14 at 01:09
  • Small k large C. My next move, when I have time, will be to start stripping code out of the page, piece-by-piece, until the case 190: starts working. If it never does, I will just have to redesign my interface. – wkb Aug 25 '14 at 01:39
  • Nope. In the code you pasted above, you have `switch (evt.keycode)`. – Bob Brown Aug 25 '14 at 01:40
  • I don't think that's it. I had tried it both ways. Your routine works, so my question is answered. I will have to explore why this won't work on my page. Thanks for your help – wkb Aug 25 '14 at 02:03
  • In your own code, put an alert before the call to faster(). Perhaps it is faster that's failing, and not the capture of the keystroke. – Bob Brown Aug 25 '14 at 02:08