Keyboard Hooking including Unicode

A

Alex Maghen

Hi. I posted this question a LONG time ago, got a response from the wonderful
Linda Liu, and then lost the thread and everything but need to close up the
issue.

I need to be able to globally hook the keyboard in a C# Windows App, but,
very specifically, I need to be able to receive a KeyPress event that will
tell me the actual CHARACTER that was pressed in the keyboard's current
state, rather than the lower-level stuff (which keyboard key, shift up/down,
etc., etc.).

In other words, I want a KeyPress event that will provide me characters such
as:
x
X
a
A
פ - a unicode character when the keyboard is in a different language
×¢ - a unicode character when the keyboard is in a different language
p
P

.... you get the idea. Linda had sent me the really nice, clean sample at
http://www.codeproject.com/KB/cs/globalhook.aspx
which gives me a really nice KeyPress event very easily that will give me
the English KeyChar, already upper or lower case, as I want it. HOWEVER, if I
switch to a different language keyboard and press a key, the event raises
KeyPress with a KeyChar == '?'. I checked the numerical value, it actually
returns a 63 which is a question mark, '?' instead of the key in the other
keyboard.

I'm so close. Can someone let me know if there's a way for me to do this
based on this example at http://www.codeproject.com/KB/cs/globalhook.aspx

Much appreciated.

Alex
 
Z

Zhi-Xin Ye [MSFT]

Dear Alex,

As far as I know to globally hook the unicode characters, we have to hook
the WM_IME_COMPOSITION message, but as explained in this KB article:

How to set a Windows hook in Visual C# .NET
http://support.microsoft.com/default.aspx?scid=kb;en-us;318804

"
Except for the WH_KEYBOARD_LL low-level hook and the WH_MOUSE_LL low-level
hook, you cannot implement global hooks in the Microsoft .NET Framework. To
install a global hook, a hook must have a native DLL export to inject
itself in another process that requires a valid, consistent function to
call into. This behavior requires a DLL export. The .NET Framework does not
support DLL exports. Managed code has no concept of a consistent value for
a function pointer because these function pointers are proxies that are
built dynamically.
"

We can only perform the WH_KEYBOARD_LL and WH_MOUSE_LL low-level hook in
the C# Windows application, the WH_GETMESSAGE hook type is not supported in
the .NET Framework.
So, I think we cannot globally hook the unicode characters in a C# Windows
application. Instead, we can create a separate DLL module, and perform the
WH_GETMESSAGE hook to trap the WM_IME_COMPOSITION message. Once the
WM_IME_COMPOSITION message is trapped, we can call the
ImmGetCompositionString API to retrieve the unicode characters.

For more information about the global hooks and WM_IME_COMPOSITION message,
you can refer to:

Hooks and DLLs
http://www.codeproject.com/KB/DLL/hooks.aspx

WM_IME_COMPOSITION
http://msdn.microsoft.com/en-us/library/ms776172.aspx

ImmGetCompositionString function
http://msdn.microsoft.com/en-us/library/ms776186.aspx

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top