1

It seems like onkeyup doesn't fire while holding command (at least in chrome on a mac). Is there anyway to get when any key is let go (onkeyup) while holding command?

Observe below:

If you hold command then press a character key, when you let of the character key, an onkeyup event doesn't fire.

document.addEventListener('keyup', (e) => {
  const {key, metaKey} = e;
  console.log({key, metaKey});
})

Specifically, I'm trying to see if the user is about holding z in while performing an Cmd + z keyboard shortcut. In my UI, I also have a clickable undo button. While the user is holding cmd + z, I want to style the button in an :active state but when they let go of z (while still possibly holding cmd), I want to remove that active state.

To clarify: I know how to check for a cmd + z in a keyboard event. Now I want an event after the user presses cmd + z to let me know when they let go of z while they could still be holding cmd.

To do that, I thought I could listen for keyup events but the keyup event doesn't seem to fire while holding cmd.

What am I missing and how can I get around this?

Rico Kahler
  • 17,616
  • 11
  • 59
  • 85
  • This might be helpful https://stackoverflow.com/questions/16006583/capturing-ctrlz-key-combination-in-javascript – Alex Bieg Jul 21 '17 at 17:58
  • @AlexBieg thanks for the reference but that's not what I'm looking for. I know how to check for a `cmd + z` in a keyboard event but what I'm trying to do now is see if the user is holding `z` or not while also holding `cmd`. I apologize, it's subtle difference. – Rico Kahler Jul 21 '17 at 18:02
  • Is cmd on mac similar to the windows key? If so I imagine the OS is intercepting it, that's probably not something you can get around. – phuhgh Jul 21 '17 at 18:20
  • @phuhgh yeah it is and i think that's the case :( – Rico Kahler Jul 21 '17 at 18:22
  • Does this answer your question? [Why does Javascript drop keyUp events when the metaKey is pressed on Mac browsers?](https://stackoverflow.com/questions/11818637/why-does-javascript-drop-keyup-events-when-the-metakey-is-pressed-on-mac-browser) – alexandergunnarson Jun 02 '21 at 15:26

0 Answers0