1

I don't appear to be able to use the onkeyup event to detect when modifier keys, specifically the Alt key, is being released, reliably. Sometimes it works, sometimes it doesn't. Most of the time it doesn't, though.

My current code is:

document.documentElement.onkeyup = function(e) {
    e = e || window.event;
    if( !e.altKey) {
        // do stuff here
        document.documentElement.onkeyup = null;
    }
}

Possibly related to Prevent default event action not working...? as I'm working in IE9 and the File menu pops up. I do dismiss the menu before attempting to trigger the event, though.

Community
  • 1
  • 1
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • I was gonna say, the window has to be active, I would assume, in order to capture the key input. If the modifier key has another function in the web browser, the keyup actions may not be consistent cross-browser. Specifically how ALT brings up menus, or Win+key pulls focus away from the browser. – donutdan4114 Oct 29 '11 at 20:20
  • That's why I think my other question (linked to) is relevant, because I'm trying to prevent the default of the menus coming up, but it's not working. – Niet the Dark Absol Oct 29 '11 at 20:23

2 Answers2

2

Not directly an answer to your question, but this might help you. It is a very detailed description on how browsers manage keydown/press/up.

johndodo
  • 17,247
  • 15
  • 96
  • 113
-1

I believe that typically a browsers key events take precedence over page defined ones. However, I would suggest using jQuery because I was just testing in IE9 and they seem to have overcome that problem.

Edit: While this seems to capture the event, I don't think it's possible to prevent IE from performing it's own events.

Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
  • 2
    Sorry, but I am resolutely against using jQuery or any other framework. Call me old-fashioned but I write my JavaScript pure. – Niet the Dark Absol Oct 29 '11 at 20:29
  • @Kolink: you should try jQuery, after half an hour of coding in it you will never go back. No, make that 15 minutes. :) IMHO, of course. – johndodo Oct 29 '11 at 20:33
  • @Kolink Fair enough, nothing wrong with that. Have you tried calling e.preventDefault() onkeydown? That might stop the menu from popping up. – Mike Cluck Oct 29 '11 at 20:38
  • @Neurotrace I've just gone and tried that. No luck, the menu still comes up. – Niet the Dark Absol Oct 29 '11 at 20:56
  • @Kolink Sorry, I think you're out of luck. I'm relatively certain that JS can't override the browser like that. This post hints at that https://forums.oracle.com/forums/thread.jspa?threadID=2199650 – Mike Cluck Oct 29 '11 at 21:37