I've started yet another useless project that'll likely never be[snip]
completed. I want to make an APL interpreter where the language
actually looks like the 1962 APL book. I thought the browser
would be an easy environment to whip something up given the
easy access to Unicode characters and complex layout like
superscripts and subscripts.
But I can't figure how to reliably capture key strokes using either
ctrl- or alt- modifiers. Failing that, I thought I could use CapsLock
or NumLock as an alternative "bucky bit" but I can't seem to get that
to work either. With ctrl- and alt- most of the alphabet is already
mapped to browser actions and they're still propagating even
though I'm returning false which I thought should cancel the event propagation.
Is there a context I can set up to capture Control or Alt keys without triggering the associated built-in browser action?
On Mon, 17 Jan 2022 19:51:11 -0800 (PST), luserdroog wrote:
Is there a context I can set up to capture Control or Alt keys without triggering the associated built-in browser action?[snip]
First, make sure to add a capturing event listener (passing `true` as the second argument of `addEventListener()` method).
In the event handler, it should check for the main key of the keyboard shortcut, not the modifier keys. e.g. for CTRL+P, check for the "P" key (preferrably using the event's `code` property), then check the modifier keys using the `ctrlKey` property. Also check other modifier key properties to make sure they're not being pressed (otherwise CTRL+SHIFT+P will also matches the condition).
After the condition is met, call the event's `preventDefault()` method to disable browser's default key handler, in case the pressed key or key combination triggers browser's built in feature.
Also call the event's `stopImmediatePropagation()` and `stopPropagation()` methods in case page scripts or browser extension scripts also use the same keyboard shortcut. However, it may not have any effect if their event listeners are also capturing event listeners, and were added before yours. i.e. first come, first serve. So, their event listeners are called first, before yours. Thus, you'll have no chance of preventing the event to be passed to their event listeners.
|Location:||Huddersfield, West Yorkshire, UK|
|Nodes:||16 (2 / 14)|