I've got a user interface that provides some functionality that recognizes whether or not the alt key is down to determine some alternative functions when clicking and otherwise interacting with the user interface. However, since there is usually no alt key on a mobile device, there is also a graphical area on the screen that can be tapped like a button to toggle the alternate functionality. The appearance of the element changes to indicate whether or not the alternate functionality is engaged. For consistency, this element also changes appearance when the alt button is down, and the state of this element is what is consulted to determine whether or not to invoke the alternate functionality.
The problem is, it seems like in certain cases there are no notifications that the alt key is released and thus the visual representation is incorrect. For instance, when the user alt+tabs away from the window, there are no key events to indicate the alt key has been released. To work around this, the onblur event of the window is used to mark the alt key as released, as the user cannot alt+tab back without releasing the alt key and if the alt key is held down while bringing the window to focus by some other means, the onkeydown
event is firing multiple times.
However, there are other ways the alt key can be used that I cannot figure out how to detect the release of. For instance, in Chrome if I hit alt+space and then click on the window to dismiss the window menu, there are no key events whatsoever that I can even check the altKey flag for and find it false. The same is true for alt+f and even alt+shift+i as long as the alt key isn't released before the dialog box appears. Worse still, none of these is even firing any blur events on window, document, or document.body, even though I clearly don't have focus any more. I can in some cases use something like onclick
or as @trincot notes, onmousemove
to detect alt being released in some cases, but not all.
I considered starting some sort of polling when the alt key is pressed and checking if it has been released, but I can't find any way to detect if the alt key is down outside of a key event.
How can I detect without fail when the status of the Alt key changes?