w3c / uievents

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

KeyboardEvent.getKeyState() #374

Closed polywock closed 4 months ago

polywock commented 7 months ago

Problem

Current web apps and games have trouble supporting non-modifier combination keys as it's difficult to determine which keys are currently pressed down.

For modifier key combinations like Shift A, it's straightforward as you can check KeyboardEvent.shiftKey or KeyboardEvent.getModifierState.

Listening to other types of combinations like A + S requires non-trivial amount of setup and testing (edge cases, extension interference, etc).

Proposal

A method that tells you if a non-modifier key is currently pressed down. Similar to KeyboardEvent.getModifierState.

KeyboardEvent.getKeyState({key: "a"}) 

KeyboardEvent.getKeyState({code: "KeyA"})
laughinghan commented 4 months ago

Can you give examples of platform APIs (Windows, Java, etc) designed this way?

polywock commented 4 months ago

Platform APIs are not designed this way. Often, there's little need for complex combinations for most standard applications or OS shortcuts. Typically only games or complex apps (video editing software, DAWS, 3D editors, etc) would need this, and so it's usually implemented by the devs themselves, or for games, by the game engine.

To implement, you would keep a set of all keys currently down, and on keyup, you will remove it from the set. For the web, this approach isn't viable as third party web extensions can interfere by preventing propagation, and in general, it's pretty hard to test properly.

laughinghan commented 4 months ago

For the web, this approach isn't viable as third party web extensions can interfere by preventing propagation.

The capturing phase happens before the bubbling phase, so couldn't you set a keyboard event handler at the top level (Window? Document?) in the capturing phase, so no stopPropagation could interfere with it?

polywock commented 4 months ago

Often that's fine, but sometimes when extensions want to support shortcut keys to activate various features of the extension, they will add a listener to the window and do stopImmediatePropogation(). That way they prevent the website from reacting to the event.

laughinghan commented 4 months ago

Sounds like an obscure case; I have difficulty imagining a well-behaved extension doing that. Can you give an example of a popular extension and shortcut where this happens, and why you'd want to know about the shortcut even though the extension handled it?

polywock commented 4 months ago

Not obscure at all and pretty much common practice for certain types of extensions. I do extension development and if you're working on an extension that runs on all websites, you must try to minimize all factors that can impact your extension. Saves a ton of maintenance time.

laughinghan commented 4 months ago

Gotcha, so it should be easy to list several examples then?

polywock commented 4 months ago

I'll just list mine as I spoke from personal experience. Four million users so not obscure at all. However, laughing right now because I'm pretty much pointing at myself for being part of the problem. Again, since my extension needs to run on all websites, I have to do this to stay sane.

https://microsoftedge.microsoft.com/addons/detail/global-speed/mjhlabbcmjflkpjknnicihkfnmbdfced

laughinghan commented 4 months ago

Very cool. What is the shortcut in question that the extension would like to suppress but you'd like the browser to still expose to the webpage?

polywock commented 4 months ago

Thanks! Nothing in particular. In fact, I'm no longer interested in this proposal. I should probably close this issue. Apologies for the trouble.