violentmonkey/vm-shortcut

`isInput` function is incomplete

Closed this issue · 4 comments

This is a followup to my comment on #6.

test.user.js.txt
While it might be good enough to use in the VM UI, as an example, isInput fails to detect at least one situation where folks would want to disable keys: When isContentEditable === true.

Attached is a simple userscript that demonstrates this problem. After installation, it will create three editable areas that keystroke would typically want to be disabled: input, textarea, and an element with contentEditable. It will then capture the keys a, b and c, with different options, logging when each is pressed.

As the various area are brought into focus, a will always be active (no options). b will not be active in the input and textarea elements, but will be active in the editable div because it uses the original isInput function. c will work correctly in all three environments as it users the enhanced isInput2 to disable capture.

isInput is just an example to demonstrate how to use conditional shortcuts. It's up to you how to implement it.

To make it clear, it's out of the scope of this library.

Maybe I should just remove it from the README. 😂

Heh. Or include the simple example.

Just to capture something semi-related here for anyone else who goes hunting in the future.

It turns out that in blur isn't always sent anyway.

If an element is removed while in focus, no blur or focusout event is sent. Thus causing inputFocus to stay true. At least until the user stumbles onto another field then leaves it to trigger a blur.

For example, user enters a popup to type in text, but hits ESC instead to dismiss it and the UI framework decides to destroy it.

I just demoed that to myself by taking the test.user.js from my first message and adding
setTimeout(() => {e.target.remove();}, 3000);
to the focus listener and watching events go by (or not).

Oh, looky there, it's even documented.

https://developer.mozilla.org/en-US/docs/Web/API/Element/blur_event

Note however that blur is not fired when a focused element is removed from the document.

Yeah, that's why a text editor is hard to implement. But still, it has nothing to do with this library.