Click here to Skip to main content
6,594,088 members and growing! (12,486 online)
Email Password   helpLost your password?
Languages » C# » How To     Intermediate License: The Code Project Open License (CPOL)

Processing Global Mouse and Keyboard Hooks in C#

By George Mamaladze

This class allows you to tap keyboard and mouse and/or to detect their activity even when an application runs in the background or does not have any user interface at all.
C#.NET 1.0, .NET 1.1, Win2K, WinXP, Win2003VS.NET2003, Dev
Posted:3 Jun 2004
Updated:12 Nov 2008
Views:780,745
Bookmarked:514 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
365 votes for this article.
Popularity: 12.56 Rating: 4.90 out of 5
3 votes, 0.8%
1
1 vote, 0.3%
2
2 votes, 0.5%
3
13 votes, 3.6%
4
346 votes, 94.8%
5

Introduction

This class allows you to tap keyboard and mouse and/or to detect their activity even when an application runs in the background or does not have any user interface at all. This class raises common .NET events with KeyEventArgs and MouseEventArgs, so you can easily retrieve any information you need.

Background

There are a number of applications that run in the background and detect user inactivity to change their mode. For example, MSN Messenger (or any other messenger). I was going to write such an application, so I searched MSDN and found "exactly" what I needed: 318804 - HOW TO: Set a Windows Hook in Visual C# .NET. This article describes how to tap the mouse movement, but it works only when an application is active. At the end of this article, I found this explanation: "Global hook is not supported in .NET Framework. You cannot implement global hooks in Microsoft .NET Framework...". Anyway, I continued my research and found out that there are exceptions. There are WH_KEYBOARD_LL and WH_MOUSE_LL hooks that can be installed globally. So, I have basically replaced WH_MOUSE with WH_MOUSE_LL in the MSDN example, and it works.

The second step was to extract the information received into a .NET EventArgs and raise the appropriate events.

I found a similar article in CodeProject, under Global System Hooks in .NET by Michael Kennedy, but what I dislike is, there is an unmanaged DLL in C++ that is a main part of this solution. This unmanaged DLL is in C++, and a number of classes make it complicated to integrate it in my own tiny application.

Revisions

This article was posted in 2004 and updated in 2006. During all this time until now I receive a lot of positive feedback and recommendations. There were also a number of technology improvements like .NET Framework 3.5 or Visual Studio 2008. So I have decided to update it once more.

I have refactored and improved the solution, made it more flexible, stable and efficient. But this refactoring also had some drawbacks:

  1. Number of code lines and files has grown.
  2. Backward compatibility to older .NETs is lost.

That's why I attend to leave the old version also to be available for download.

Using the Code [Version 2]

The Simple Way

If you are developing a Windows Forms application and prefer drag & drop programming, there is a component named GlobalEventProvider inside the assembly Gma.UserActivityMonitor.dll. Just drag and drop it to your form and create events using the property editor events tab.

The Alternative Way

Use events provided by the static class HookManager. Note that the sender object in events is always null.

For more usage hints, see the source code of the attached demo application.

Using the Code [Version 1]

To use this class in your application, you need to just create an instance of it and hang on events you would like to process. Hooks are automatically installed when the object is created, but you can stop and start listening using appropriate public methods.

UserActivityHook actHook;
void MainFormLoad(object sender, System.EventArgs e)
{
    actHook= new UserActivityHook(); // crate an instance

    // hang on events

    actHook.OnMouseActivity+=new MouseEventHandler(MouseMoved);
    actHook.KeyDown+=new KeyEventHandler(MyKeyDown);
    actHook.KeyPress+=new KeyPressEventHandler(MyKeyPress);
    actHook.KeyUp+=new KeyEventHandler(MyKeyUp);
}

Now, an example of how to process an event:

public void MouseMoved(object sender, MouseEventArgs e)
{
    labelMousePosition.Text=String.Format("x={0}  y={1}", e.X, e.Y);
    if (e.Clicks>0) LogWrite("MouseButton     - " + e.Button.ToString());
}

Changes and Updates from [Version 0] to [Version 1]

I'd like to thank you all for all the useful comments in the discussion forum. There were a lot of bugs and proposals posted after this article was published on 4th June, 2004. Over and over again came the same topics, and I had to refer to previous posts in the discussion, that is why I have decided to revise the code and publish a FAQ. Here is the list of the most important changes:

  • The project was converted into Visual Studio 2005
  • The problem with upper case characters is solved
  • Mouse wheel information is now included in event arguments
  • Better exception handling
  • Cancellation of keyboard events using the Handled property of event arguments
  • XML documentation of functions

FAQ [Version 1]

Question

The project cannot be run in Visual Studio .NET 2005 in debug mode because of an exception error caused by calling the SetWindowsHookEx. Why? Is it a problem of .NET 2.0?

Answer

The compiled release version works well so that cannot be a .NET 2.0 problem. To workaround, you just need to uncheck the check box in the project properties that says: "Enable Visual Studio hosting process". In the menu: Project -> Project Properties... -> Debug -> Enable the Visual Studio hosting process.

Question

I need to suppress some keystrokes after I have processed them.

Answer

Just set the e.Handled property to true in the key events you have processed. It prevents the keystrokes being processed by other applications.

Question:

Is it possible to convert your global hooks to application hooks which capture keystrokes and mouse movements only within the application?

Answer

Yes. Just use...

private const int WH_MOUSE = 7;
private const int WH_KEYBOARD = 2;

... everywhere, instead of:

private const int WH_MOUSE_LL = 14;
private const int WH_KEYBOARD_LL = 13;

Question

Does it work on Win98 (Windows ME, Windows 95)?

Answer

Yes and No. The global hooks WH_MOUSE_LL and WH_KEYBOARD_LL can be monitored only under Windows NT/2000/XP. In other cases, you can only use application hooks (WH_MOUSE and WH_KEYBOARD) which capture keystrokes and mouse movement only within the application.

Question

I have a long delay when closing applications using hooks by clicking the x button in the titlebar. If I close the application via another event (button click) for example, that works fine.

Answer

It's a known bug of Microsoft. It has to do with the Windows themes. If you disable the Windows themes, the problem goes away. Another choice is to have the hook code run in a secondary thread.

Question

How do I catch key combinations like Ctrl+Shift+A?

Answer

You'll have to track which keys have gone down but not up. Only the most recently pressed key keeps sending KeyDown messages, but the others will still send a KeyUp when released. So if you make three flags IsCtrlDown, IsShiftDown, and IsADown, and set them to true at KeyDown and false at KeyUp, the expression (IsCtrlDown && IsShiftDown && IsADown) will give you the required result.

Question

Does it works with .NET Framework 1.1 and Visual Studio 2003?

Answer

Yes. The file UserActivityHook.cs can be used without any changes, in a Visual Studio 2003 .NET 1.1 project.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

George Mamaladze


Member

Occupation: Software Developer (Senior)
Location: Germany Germany

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 904 (Total in Forum: 904) (Refresh)FirstPrevNext
General.. Pingroupyuphi12hrs 21mins ago 
Generaldemo application version two (.exe) now fails when run on Win Vista SP2 [modified] PinmemberBillWoodruff17:41 6 Nov '09  
AnswerRe: demo application version two (.exe) now fails when run on Win Vista SP2 PinmemberGeorge Mamaladze21hrs 15mins ago 
QuestionWhere to put conditions based on key that is pressed Pinmemberfreedo513:28 28 Oct '09  
GeneralWin32Exception always occurs PinmemberJordanwb6:10 26 Oct '09  
Generalfyi : Global Hook not usable in Visual Studio 2010 beta 2 compiled against FrameWork 4.0 PinmemberBillWoodruff1:32 22 Oct '09  
GeneralRe: fyi : Global Hook not usable in Visual Studio 2010 beta 2 compiled against FrameWork 4.0 PinmemberGeorge Mamaladze1:34 22 Oct '09  
GeneralRe: fyi : Global Hook not usable in Visual Studio 2010 beta 2 compiled against FrameWork 4.0 PinmemberBillWoodruff2:32 22 Oct '09  
GeneralRe: fyi : Global Hook not usable in Visual Studio 2010 beta 2 compiled against FrameWork 4.0 Pinmembermattiask11:08 22 Oct '09  
GeneralRe: fyi : Global Hook not usable in Visual Studio 2010 beta 2 compiled against FrameWork 4.0 PinmemberBillWoodruff15:45 22 Oct '09  
GeneralRe: fyi : Global Hook not usable in Visual Studio 2010 beta 2 compiled against FrameWork 4.0 Pinmembermattiask18:56 22 Oct '09  
GeneralRun in background Pinmembersakshismriti21:33 12 Oct '09  
GeneralGma.UserActivityMonitor.dll PinmemberStefan Schierle0:45 2 Oct '09  
AnswerRe: Gma.UserActivityMonitor.dll PinmemberGeorge Mamaladze1:19 2 Oct '09  
GeneralRe: Gma.UserActivityMonitor.dll PinmemberStefan Schierle4:02 2 Oct '09  
AnswerRe: Gma.UserActivityMonitor.dll PinmemberGeorge Mamaladze4:05 2 Oct '09  
GeneralRe: Gma.UserActivityMonitor.dll PinmemberS078420:03 4 Oct '09  
GeneralWindows themes bug PinmemberJoan Pedersen7:11 29 Sep '09  
GeneralRe: Windows themes bug PinmemberGreg Cadmes14:50 29 Sep '09  
GeneralUpdates to code Pinmembermoonpond23:50 28 Sep '09  
GeneralBlocked by antivirus software Pinmemberhjy121016:48 18 Sep '09  
GeneralThanks for Share! Pinmemberyun_feiyang18:06 17 Sep '09  
Generaldemo 2 works fine in Visual Studio 2010 beta compiled against Framework 4.0 PinmemberBillWoodruff3:18 14 Sep '09  
GeneralIs there any way to capture the keystrokes after disabling the keyboard.. Pinmemberf20034333:31 10 Sep '09  
GeneralDemo v2: Exception PinmemberdenizKartan11:22 8 Sep '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Nov 2008
Editor: Deeksha Shenoy
Copyright 2004 by George Mamaladze
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project