w3c / uievents

UI Events
https://w3c.github.io/uievents/
Other
145 stars 52 forks source link

Should we dispatch a keypress event when spacebar is used to activate an element #225

Open dbates-wk opened 5 years ago

dbates-wk commented 5 years ago
  1. Open the following example page in a browser:
    
    <!DOCTYPE html>
    <html>
    <body>
    <input type="checkbox">
    <pre id="console"></pre>
    <script>
    let checkbox = document.querySelector("input");

function logKeyEvent(event) { logMessage(${event.type} key: ${event.key} keyCode: ${event.keyCode}); }

function logEvent(event) { logMessage(${event.type}); }

function logMessage(message) { document.getElementById("console").appendChild(document.createTextNode(message + "\n")); }

function handleFocus() { checkbox.addEventListener("keydown", logKeyEvent); checkbox.addEventListener("keypress", logKeyEvent); checkbox.addEventListener("keyup", logKeyEvent); checkbox.addEventListener("change", logEvent); checkbox.addEventListener("click", logEvent); checkbox.addEventListener("DOMActivate", logEvent); }

checkbox.addEventListener("focus", handleFocus, { once: true });


2. Focus the checkbox. On Mac Safari and Chrome Version 74.0.3725.0 (Official Build) canary (64-bit) you can do this by pressing Option + Tab. On Mac Firefox 65.0.1 , just press Tab.

3. Press the spacebar to activate.

What events should be dispatched?

Safari and Chrome have identical output,  ignoring the initial keyups for the Option + Tab keys (used to focus the checkbox), emphasis mine:

keydown key: keyCode: 32 keypress key: keyCode: 32 keyup key: keyCode: 32 click change


But Firefox outputs, ignoring the initial keyup for the Tab key (used to focus the checkbox), emphasis mine:

keydown key: keyCode: 32 keypress key: keyCode: 0 keyup key: keyCode: 32 click DOMActivate change



All browsers fire a keypress event. Though Firefox also dispatches a DOMActivate event. The spec. does not seem to say what is the correct events and event ordering. The wording in <https://w3c.github.io/uievents/#legacy-uievent-event-order> says that if you fire a DOMActivate (like Firefox) then it seems that you don't fire a keypress. Firefox is not abiding by this. But the spec does not seem to mention how to handle activation if a DOMActivate event is ***not*** dispatched like in Chrome and Safari. Can we please spec this behavior out clearly?
dbates-wk commented 5 years ago

@annevk @travisleithead @garykac @hober @rniwa

travisleithead commented 5 years ago

Totally seems like firing the keypress event is a good spec change to make; however, you are correct in observing that event order is less-well defined. I think adding event ordering, etc., is a longer-term refactoring of the spec, but perhaps there's a tactical spec change to make? Also, thanks for the test page!

masayuki-nakano commented 5 years ago

The wording in https://w3c.github.io/uievents/#legacy-uievent-event-order says that if you fire a DOMActivate (like Firefox) then it seems that you don't fire a keypress.

No, keypress event should be fired for every key press of printable keys and Enter key unless preceding keydown event is consumed. The reason is, activation may occur as a default action of keypress events.

dbates-wk commented 5 years ago

No, keypress event should be fired for every key press of printable keys and Enter key unless preceding keydown event is consumed. The reason is, activation may occur as a default action of keypress events.

Spec. fix please to make this clear.