user541686
user541686

Reputation: 210725

How to circumvent the "foreground lock timeout" to activate my window?

I'm trying to make an application like Launchy/Enso/etc., which pops up when the user presses the Caps Lock key.

To do this, I have needed to install a low-level keyboard hook (WH_KEYBOARD_LL), from which I subsequently spawn a thread to display the dialog to present to the user.

The trouble is, when I somehow steal focus (e.g. by clicking on another window) and subsequently press Caps Lock with a short delay, my window doesn't get the keyboard input: the input goes to the background window, even though my window is "active" (from looking at the title bar).

Of course, this gets pretty annoying, since I then end up typing something like "visu" (for "Visual Studio") inside a text processor (or a chat box...) and pressing Enter, withotu realizing that it didn't do what I intended.

(Funny thing is, after a 3-second (or so) pause, my window's title bar suddenly becomes "inactive", even though it was never active in the first place!)

How can I bypass this "protection" mechanism to actually activate my window?

Upvotes: 4

Views: 3401

Answers (1)

user541686
user541686

Reputation: 210725

Okay, I finally figured out a hack. (Microsoft employers: please look away...)

I intercept Caps Lock with a low-level keyboard hook, then when I detect VK_CAPITAL, I call

keybd_event(
    VK_OEM_8,
    (BYTE)MapVirtualKey(pKBDLLHook->vkCode, MAPVK_VK_TO_VSC),
    (wParam == WM_KEYUP || wParam == WM_SYSKEYUP) ? KEYEVENTF_KEYUP : 0,
    0
);

from within the handler.

Essentially, I just change the request to VK_OEM_8.
However, Notice that the virtual-key code does not correspond to the scan code. This is intentional -- VK_OEM_8 doesn't have a scan code (as far as I could tell, anyway) so I didn't have much of an option.

Then I just program based on VK_OEM_8 instead -- which is easy to intercept/handle/etc.

Hope this helps out other people.

Upvotes: 1

Related Questions