-2

I have a function with "onkeyup" event:

document.onkeyup=function(e){
  var e = e || window.event;
  if(e.altKey && e.which == 73) {
    alert('ALT+i has been pressed');
    return false;
  }
}

That works in IE 11,Opera. In Firefox it doesn't work. There are similar questions over here, however all the answers I've found, based on use "Keyboard​Event​.which" and "KeyboardEvent.keyCode" As far as I know, "Keyboard​Event​.which" has been deprecated along with "KeyboardEvent.keyCode". What would be the proper way to implement this method using Javascript? If possible without "addEventListener("keyup"...)" method.

Serg
  • 151
  • 10
  • doesn't work means what? Just no event fires? Or an error in the console, or what? You mention deprecation in the title but then don't say more about it. I'm not aware of any reason why this wouldn't work in Chrome, though. The [documentation](https://developer.mozilla.org/en-US/docs/Web/API/Document/keyup_event) has a compatibility table which doesn't suggest any issues. And what's your objection to addEventListener? That's the conventional, modern, cross-browser way to declare an event handler. – ADyson Apr 26 '19 at 19:45
  • First, you should be using `addEventListener()` for all event binding as it is the more robust way to set up events. Second, `keyup` works just fine in Chrome as you can see by running the code in your own question. Voting to close as unreproduceable. – Scott Marcus Apr 26 '19 at 19:46
  • `event.key` is the key here. And returning `false` from an event listener does nothing. If you want to prevent the default action, you should do `e.preventDefault();`. + FF doesn't allow to override some of the browser's short-cuts. – Teemu Apr 26 '19 at 19:49
  • My fault - it works in Crome not in Firefox. Post corrected.In FireFox "alt"+I opens windows explorer – Serg Apr 26 '19 at 19:49
  • 1
    @Serg How is CTRL+I related to the question? – Teemu Apr 26 '19 at 19:54
  • your code is detecting Alt not Ctrl... – ADyson Apr 26 '19 at 19:54
  • And Ctrl+I doesn't open windows explorer from Firefox. It does a number of things, depending on context, but not that. See https://www-archive.mozilla.org/docs/end-user/moz_shortcuts.html for a list of what it _does_ do. (Also, how would that work on a non-Windows device, do you suppose?) – ADyson Apr 26 '19 at 20:00
  • My mistake - "alt"+i opens window explorer instead of showing"alert". – Serg Apr 26 '19 at 20:02
  • Not when I tested it in Firefox just now, it didn't. Your code worked fine. Have you configured some custom shortcut in your browser or operating system? Try testing on another device, if you can, or use something virtual like browserstack or similar, or even just a VM with a fresh install. – ADyson Apr 26 '19 at 20:05
  • No, I tested on different computers, result the same "alert" is not showing up, – Serg Apr 26 '19 at 20:09
  • I found out that it "fires" in FF after some time delay after page loaded in FF 62.0 in FF 66.03 Quantum - it still shows explorer . – Serg Apr 26 '19 at 20:11
  • I dunno what to tell you, it works fine for me, instantly (on a blank page with nothing else present). Firefox 66, Windows 7. And Alt+I does not, and never has, opened Windows Explorer or any other application whilst using Firefox. Which is why I thought you must have some kind of customised setup. – ADyson Apr 26 '19 at 20:14
  • Just for kicks, why not test some other key combo, or just a single key press? Or just get it to log whatever you press to the console, regardless? Then you'll have a better idea whether it's the _event_ which is at fault, or your specific key combination - or even the use of alert() can occasionally change things, better to use console.log() instead for debugging – ADyson Apr 26 '19 at 20:15

1 Answers1

0

Solved! It happened when FF console was opened. After closing console everything works fine. After re-opening console it works also. No clue why it behaves such way. Anyway thanks to everybody and sorry for the mess.

Serg
  • 151
  • 10