locked
How do I set a low level mouse hook and keyboard hook in C#? RRS feed

  • Question

  • How do I set a low level mouse hook and keyboard hook in C#?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, April 8, 2009 3:00 AM

Answers

  • In order to set a low level mouse and keyboard hook in C#, we P/Invoke Windows API SetWindowsHookEx, CallNextHookEx, UnhookWindowsHookEx, and GetModuleHandle. 

    The first parameter of SetWindowsHookEx specifies the type of hook procedure to be installed.  Here we use WH_KEYBOARD_LL or WH_MOUSE_LL .  
    The second parameter is the pointer to the hook procedure. In C# we pass the delegate of the hook callback function to this parameter.  
    The third parameter is the handle of the module containing the hook procedure.  Therefore, we call the GetModuleHandle to retrieve the current module handle of the C# hook application.
    The fourth parameter specifies the identifier of the thread with which the hook procedure is to be associated.  If this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread.

    To conclude the hook procedure, we use CallNextHookEx function to pass the hook information to the next hook procedure in the current hook chain.  

    When we want to stop the low level mouse or keyboard hook, we can use the UnhookWindowsHookEx function to remove the hook procedure installed.   The parameter is the hook ID we get from the SetWindowsHookEx function.

     

    Here is an article from the Microsoft official website on this topic:
    How to set a Windows hook in Visual C# .NET.

    Here is a MSDN blog article on low level mouse hooks in C# by Stephen Toub:
    http://blogs.msdn.com/toub/archive/2006/05/03/589468.aspx

    Here is another MSDN blog article on low level keyboard hooks in C# by Stephen Toub:
    http://blogs.msdn.com/toub/archive/2006/05/03/589423.aspx

    Here is an article on the same topic by George Mamaladze:
    http://www.codeproject.com/KB/cs/globalhook.aspx

     

    For more FAQ about Visual C# General, please see Visual C# General FAQ

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

    Wednesday, April 8, 2009 3:00 AM

All replies

  • In order to set a low level mouse and keyboard hook in C#, we P/Invoke Windows API SetWindowsHookEx, CallNextHookEx, UnhookWindowsHookEx, and GetModuleHandle. 

    The first parameter of SetWindowsHookEx specifies the type of hook procedure to be installed.  Here we use WH_KEYBOARD_LL or WH_MOUSE_LL .  
    The second parameter is the pointer to the hook procedure. In C# we pass the delegate of the hook callback function to this parameter.  
    The third parameter is the handle of the module containing the hook procedure.  Therefore, we call the GetModuleHandle to retrieve the current module handle of the C# hook application.
    The fourth parameter specifies the identifier of the thread with which the hook procedure is to be associated.  If this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread.

    To conclude the hook procedure, we use CallNextHookEx function to pass the hook information to the next hook procedure in the current hook chain.  

    When we want to stop the low level mouse or keyboard hook, we can use the UnhookWindowsHookEx function to remove the hook procedure installed.   The parameter is the hook ID we get from the SetWindowsHookEx function.

     

    Here is an article from the Microsoft official website on this topic:
    How to set a Windows hook in Visual C# .NET.

    Here is a MSDN blog article on low level mouse hooks in C# by Stephen Toub:
    http://blogs.msdn.com/toub/archive/2006/05/03/589468.aspx

    Here is another MSDN blog article on low level keyboard hooks in C# by Stephen Toub:
    http://blogs.msdn.com/toub/archive/2006/05/03/589423.aspx

    Here is an article on the same topic by George Mamaladze:
    http://www.codeproject.com/KB/cs/globalhook.aspx

     

    For more FAQ about Visual C# General, please see Visual C# General FAQ

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

    Wednesday, April 8, 2009 3:00 AM
  • Introduction and Reference:

    A hook is a point in the system message-handling mechanism where an application can install a subroutine to monitor the message traffic in the system. This is where certain types of messages will be processed before they reach the target window procedure.   For a detailed introduction on hooks, please refer to http://msdn.microsoft.com/en-us/library/ms632589(VS.85).aspx

    To set a Windows hook, we call the Windows API SetWindowsHookEx from the user32.dll.   This function installs an application-defined hook procedure in the system hook chain associated with the certain hook type.   We use the Windows API CallNextHookEx from the user32.dll to pass the hook information to the next hook procedure in the current hook chain. The Windows API UnhookWindowsHookEx is used to remove a hook procedure installed in a hook chain by the SetWindowsHookEx function.

    Note:  Global hooks are not supported in the .NET Framework

    Except for the WH_KEYBOARD_LL low-level hook and the WH_MOUSE_LL low-level hook, we cannot implement global hooks in the Microsoft .NET Framework.  

    To install a global hook, a hook must have a native DLL injecting itself into 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 function pointers because these function pointers are proxies that are built dynamically.

    For more details, please refer to http://support.microsoft.com/default.aspx/kb/318804.

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, April 8, 2009 3:02 AM