-1

I have a bit of an odd question pertaining to Windows: is there any way to globally determine the last time that any key, or in particular, the modifier key (aka alt) was pressed without resorting to drivers or kernel-mode code?

Some background: I have registered a global shortcut (alt+`) and successfully run code when it is executed. Currently, I use some heuristics that are very much fallible to determine if the user is repeatedly pressing and releasing the backtick key while the alt key is consistently held down or if the user has pressed and released both since the last time my hotkey handler was called.

I wish to more-precisely ascertain whether or not the alt key has been released since the last time my handler was called. Being able to uniquely identify the time of the last alt keypress is an obvious solution. Another is somehow hooking on to each alt key press to record that info, which I do not believe is possible.

I'm open to all ideas and suggestions!

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
  • 3
    almost sure windows not save internally time when some key was pressed. you can call `SetWindowsHookEx(WH_KEYBOARD_LL,)` and internally monitor key press and time – RbMm Mar 05 '17 at 20:13
  • A low-level keyboard hook **is** a global hook. It hooks input events before the system even determined, which window the input should go to. – IInspectable Mar 05 '17 at 21:50
  • @RbMm I'll be happy to accept it as an answer if you post it, since you mentioned this before IInspectable did. – Mahmoud Al-Qudsi Mar 09 '17 at 16:52

1 Answers1

1

You can install a global low-level keyboard hook (by calling SetWindowsHookEx, passing a LowLevelKeyboardProc). This allows you to globally monitor the WM_KEYUP event for the VK_MENU, VK_LMENU and/or VK_RMENU key. Together with the timestamp recorded in the hotkey handler you can determine, whether the hotkey is part of the same Alt sequence or a new one: If the timestamp of the WM_KEYUP event is larger than previous hotkey input, the user started a new Alt+` sequence, otherwise it's a continuation of the same sequence.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • I'm afraid you misunderstood my question. I'm already using `RegisterHotKey` and it's working perfectly. I'm trying to obtain additional information past the functionality that this API provides. – Mahmoud Al-Qudsi Mar 05 '17 at 21:46
  • @MahmoudAl-Qudsi: Indeed, I did. And even with your clarification I cannot follow. If you have set up a global hot key, it calls into your handler whenever it is pressed. You can store the last time it was pressed in your handler. Why aren't you? – IInspectable Mar 05 '17 at 21:49
  • I am storing the time, of course. I have a hotkey set up for alt+` but the hotkey callback is (obviously) called in the same way regardless if the user presses and release alt and backtick or if the user holds down alt and presses and releases only the backtick repeatedly. I need to distinguish the two to provide different results. Best example is alt-tab, if you alt-tab, alt-tab the behavior is different from alt-tab, tab. – Mahmoud Al-Qudsi Mar 05 '17 at 22:07