Illya Moskvin
Illya Moskvin

Reputation: 342

GetMessage(WM_CHAR) override? Making sure all other Window Hooks activate

I'm looking for a way to intercept a GetMessage() call for WM_CHAR in an external process (only the active window, to be specific). I'm hesitant to use SetWindowsHookEx() - I want to make absolutely sure that the widest possible range of programs which may modify keyboard input have a chance to fire first. Does anyone have any suggestions?

Context: I'm building an application meant to generate a visual of the current keyboard layout. However, I'm looking to make it as compatible with third-party keyboard remapping software as possible (including user-made AutoHotkey scripts and the like). When triggered via hotkey, my program sends key-presses to the active window (scancodes + appropriate virtual keys), which should get processed by its window procedure... thus triggering both any remap software based on system-wide hooks and that which has been programmed to only remap keys within the active application... and then my program would intercept the resulting WM_CHAR / WM_UNICHAR / WM_DEADCHAR message, from which it reconstructs the keyboard layout. In effect, the character messages should never reach the active window for output. (I might replace them with no char messages of some kind for compatibility.)

P.S. Sorry, this is probably terrible etiquette - posting a question first thing after registering - but I've been researching this issue for many nights now, and I still haven't figured out an optimum solution! I'm working primarily from AutoHotkey, and I'm fully willing to share other methods I've considered for getting the true current keyboard layout, but they all have their limitations re: playing nice with remapping software.

Upvotes: 2

Views: 418

Answers (2)

DJm00n
DJm00n

Reputation: 1411

Windows produces WM_KEYDOWN message on each keypress/release. After it this message gets into your program, you usually call TranslateMessage API in your message look. This method is the method that generates WM_CHAR and inserts it into your message loop.

TranslateMessage uses ToUnicode API under the hood (tt also does a few extra things like processing Alt+NumPad combinations but this is not mandatory in this case).

You can use ToUnicode API yourself for each scan code to extract keyboard layout information. Better to do it once on WM_INPUTLANGCHANGE because this API have side effects and may break dead key processing.

Read this post series for a example code: http://archives.miloush.net/michkap/archive/2006/03/23/558674.html

Upvotes: 0

Michael
Michael

Reputation: 1353

Put '~' (without the quotes) before your hotkey.

This will run whatever code you have for you hotkey and allow the keystroke to fall through.

Example:

~^a::
MsgBox, You pressed Ctrl+a
return

Upvotes: 0

Related Questions