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"> </p>
<button>Dummy</button>
<input />
Say we do two test:
- Test 1 - Focus on button (or document)
- Test 2 - Focus on input
And press:
- Press and hold Shift
- Press and hold Alt
- Press and release A
- Press and release X
- Release Shift and Alt
Result:
Test 1:
modifiers(Shift) key: Shiftmodifiers(Shift + Alt) key: Metamodifiers(Shift + Alt) key: Amodifiers(Shift + Alt) key: X
Test 2:
modifiers() key: Shiftmodifiers(Shift) key: Metamodifiers(Shift + Alt) key: Amodifiers(Shift + Alt) key: X