Capturing Global Function Key Press

  • Thread starter Thread starter Alex Maghen
  • Start date Start date
A

Alex Maghen

Hi. I need to set a Global Key Hook to capture the keypress of function keys
(F9, F10, etc., etc.). I've been using a sample recommended to me here
("userActivityHook"), but my "KeyPress()" event handler does not get called
for Function Keys.

How is this done?

Thanks.

Alex
 
Hi Alex,

Thank you for using Microsoft Managed Newsgroup Service, I'm Zhi-Xin Ye,
it's my pleasure to work with you on this issue.

Using the keyword "userActivityHook" I find the sample you mentioned on
CodeProject.

In the HookManager.Callbacks.cs file, there's a method named
KeyboardHookProc:

private static int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
......
if (s_KeyPress != null && wParam == WM_KEYDOWN)
{
//......

if (ToAscii(MyKeyboardHookStruct.VirtualKeyCode,
MyKeyboardHookStruct.ScanCode,
keyState,
inBuffer,
MyKeyboardHookStruct.Flags) == 1)
{
.....
s_KeyPress.Invoke(null, e);
}
}

If you set a breakpoint at the line which calls the ToAscii API and debug
the program, you'll find that when a function key is pressed, the return
value of the ToAscii API is not 1, so it does not run to the line "
s_KeyPress.Invoke(null, e); ", so the KeyPress event does not fire. You
can change the code to fire the KeyPress event as you needed, but it's not
recommended to do so, it make no sense to fire KeyPress event for
noncharacter keys. Instead, you can handle the KeyDown or KeyUp event for
the function keys(noncharacter keys).

If you have any question or concern, please feel free to let me know. I
will be happy of assistance.

Have a nice day!

Best Regards,
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: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 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. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top