tags:

views:

132

answers:

4

How to capture key press, e.g., Ctrl+Z, without placing an input element on the page in JavaScript? Seems that in IE, keypress and keyup events can only be bound to input elements (input boxes, textareas, etc)

A: 

document.onkeydown=keyDown;

document.onkeypress=keyPress;

etc.

Works for me in IE

rob
Have you tried on IE6 and tried arrow keys?
powerboy
A: 

Code & detects ctrl+z

document.onkeyup = function(e) {
  if(e.ctrlKey && e.keyCode == 90) {
    // ctrl+z pressed
  }
}
Ben Rowe
A: 

jQuery also has an excellent implementation that's incredibly easy to use. Here's how you could implement this functionality across browsers:

$(document).keypress(function(e){
    var checkWebkitandIE=(e.which==26 ? 1 : 0);
    var checkMoz=(e.which==122 && e.ctrlKey ? 1 : 0);

    if (checkWebkitandIE || checkMoz) $("body").append("<p>ctrl+z detected!</p>");
});

Tested in IE7,Firefox 3.6.3 & Chrome 4.1.249.1064

Another way of doing this is to use the keydown event and track the event.keyCode. However, since jQuery normalizes keyCode and charCode using event.which, their spec recommends using event.which in a variety of situations:

$(document).keydown(function(e){
if (e.keyCode==90 && e.ctrlKey)
    $("body").append("<p>ctrl+z detected!</p>");
});
Trafalmadorian
didn't know e.which can capture keystroke as well as mouse click! And could you tell me how to get the keycode 26?
powerboy
keycode 26 is a specific id for "which" set by a keypress event (indicates CTRL+z). To see more character mappings for the keypress event, take a look at [the jQuery docs](http://api.jquery.com/keypress/) or [unixpapa's key event tester](http://unixpapa.com/js/testkey.html)
Trafalmadorian
Thx. I know what is the meaning of keycode. I mean, 17 is for Ctrl, 90 is for Z, then how did you get the number 26? I just googled but could not find the answer.
powerboy
Use those links I put in my comment to test out the codes for different keystrokes.
Trafalmadorian
You won't get 26 in all browsers. In fact, you only get 26 in WebKit. Not in IE, Firefox or IE.
Tim Down
@Tim Down, you can easily check for multiple key and modifier combinations across browsers. I'll edit my answer to reflect this. However, event.which definitely does apply 26 to ctrl+z combinations in IE, at least up to IE7. I'd a appreciate a vote back up if this is helpful for you.
Trafalmadorian
I'll undo the downvote, but I don't think this is the right approach: using the `keydown` event and checking its `keyCode` property will give you much more uniform results across browsers.
Tim Down
Fair enough, I'll go ahead and include that route as well in my answer. Thanks for the constructive crit :)
Trafalmadorian
A: 

For non-printable keys such as arrow keys and shortcut keys such as Ctrl-z, Ctrl-x, Ctrl-c that may trigger some action in the browser (for instance, inside editable documents or elements), you may not get a keypress event in all browsers. For this reason you have to use keydown instead, if you're interested in suppressing the browser's default action. If not, keyup will do just as well.

Attaching a keydown event to document works in all the major browsers:

document.onkeydown = function(evt) {
    evt = evt || window.event;
    if (evt.ctrlKey && evt.keyCode == 90) {
        alert("Ctrl-Z");
    }
};

For a complete reference, I strongly recommend Jan Wolter's article on JavaScript key handling.

Tim Down