4

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?

Michael
  • 9,060
  • 14
  • 61
  • 123
  • I think this is the closest you can get: https://stackoverflow.com/a/8875522/5459839 – trincot Aug 27 '17 at 20:26
  • @trincot Thanks! I notice that I can also get an `onclick` event if I click in the client area, but strangely there are still cases I can reproduce clicking elsewhere where I neither lose focus nor get any events. – Michael Aug 27 '17 at 20:29
  • 2
    I don't want to be negative, but the **alt** key is intended to modify a normal key on the keyboard. By itself it should do nothing. By using it in an unintended way you run into problems. I know this isn't helpful, but isn't there a way to make your design more compatible with in the way a keyboard was intended to be used? – KIKO Software Aug 27 '17 at 20:31
  • @KIKOSoftware I agree with you! Originally the *ctrl* key was used as a modifier but there were issues with that as well on certain platforms. – Michael Aug 27 '17 at 20:38

0 Answers0