Why is modifier keys on keydown delayed in "input"?

Viewed 38

Have added a routine that show labels with access-keys when user press Shift + Alt.

At first I tested to see if:

evt.altKey && evt.shiftKey && 
  (evt.key == Shift || evt.key == Meta)

But this does not work on (at least) some input fields as the shiftKey and altKey is not set until after / on next key press. It works on for example input type="range" but not type="number" or some sort of text in general I guess.

Easy enough to fix, but curious as to why it is this way.

  • Is it undefined behavior (I.e. can be either or)?
  • Is there some definition on this behavior?

I'm on Firefox Developer Edition 93.0b9 (64-bit) - Linux.

(Event also say key = "Meta" on alt key-press, but set altKey instead of metaKey, pressing meta (Windows-key) say "OS" and metaKey not set – but that is another question. The latter as metaKey not set when using meta is "documented" on MDN.)

Sample

Below is a simple example to run a test:

const can = document.getElementById('can');
const show = e => {
  let k = [];
  if (e.shiftKey)
    k.push('Shift');
  if (e.altKey)
    k.push('Alt');
  can.textContent = 
    'modifiers(' + k.join(' + ') + ') key: ' + e.key;
};
window.addEventListener('keydown', show);
:focus {
  outline: 1px solid red;
}
<p id="can">&nbsp;</p>
<button>Dummy</button>
<input />

Say we do two test:

  • Test 1 - Focus on button (or document)
  • Test 2 - Focus on input

And press:

  1. Press and hold Shift
  2. Press and hold Alt
  3. Press and release A
  4. Press and release X
  5. Release Shift and Alt

Result:

Test 1:

  1. modifiers(Shift) key: Shift
  2. modifiers(Shift + Alt) key: Meta
  3. modifiers(Shift + Alt) key: A
  4. modifiers(Shift + Alt) key: X

Test 2:

  1. modifiers() key: Shift
  2. modifiers(Shift) key: Meta
  3. modifiers(Shift + Alt) key: A
  4. modifiers(Shift + Alt) key: X
0 Answers
Related