Capturing Global Function Key Press

Discussion in 'Microsoft Dot NET Framework Forms' started by Alex Maghen, Feb 15, 2009.

  1. Alex Maghen

    Alex Maghen Guest

    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?


    Alex Maghen, Feb 15, 2009
    1. Advertisements

  2. 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

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

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

    if (ToAscii(MyKeyboardHookStruct.VirtualKeyCode,
    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:

    Get notification to my posts through email? Please refer to

    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
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Zhi-Xin Ye [MSFT], Feb 16, 2009
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Chapman

    On key press

    Richard Chapman, Jul 9, 2003, in forum: Microsoft Dot NET Framework Forms
    Richard Chapman
    Jul 9, 2003
  2. Guogang

    Get key press status in forms or controls

    Guogang, Jul 17, 2003, in forum: Microsoft Dot NET Framework Forms
    Jul 17, 2003
  3. Rob Oldfield

    Catch key press in a datagrid

    Rob Oldfield, Sep 25, 2003, in forum: Microsoft Dot NET Framework Forms
    Rob Oldfield
    Sep 26, 2003
  4. Kueishiong Tu

    What TextBox event to track when the user press the "enter" key

    Kueishiong Tu, Sep 26, 2003, in forum: Microsoft Dot NET Framework Forms
    Bob Powell [MVP]
    Sep 26, 2003
  5. Jeremy

    Capturing key press messages

    Jeremy, Oct 17, 2008, in forum: Microsoft Dot NET Framework Forms
    Oct 27, 2008