How To Detect Global Mouse Hooks In A C# Console Application

global mouse hooks in console apps with C#
global mouse hooks in console apps with C#

Hello friends, this week we will look at another low level programming topic with C#. We are going to do something which normally is not possible with C#. That is, Listening to global mouse hooks (Mouse clicks every where on your screen) with a C# console application. The guys who built the classes used for writing console applications surely thought handling clicks wasn’t necessary. Not to talk about clicks made out side of the console window too. This functionality is not the most important functionality one could need or even think about, but in some cases, you may need this particular feature in a software. Before going farther, I want to precise something. The tips mentioned here to build such software do not only work on console applications but also on desktop apps written in C# with .Net.

Detecting Global Mouse Hooks

This may sound super complicated to do (Yes, normally it should be), but there are tools out there which make your work a lot easier. Using this library and applying a few tricks on the console application will make you implement this functionality in no time. You should bear in mind that, console applications where surely not meant to work with clicks, so we will need Windows forms apis at some point in time. Let’s dive in.

Before we proceed, HERE is the source code for this demo.

We are going to build this demo console application by following a series of steps. These steps are mentioned here in order.

1- Create a C# Console Application

Create a simple console application and name it as you wish. This console application should target the .Net framework.

2- Add The Mouse Global Hooks nuget package

Add this package to your solution; MouseKeyHook.

3- Add a Reference to System.Windows.Forms assembly

You will need to handle click events. And some of the base classes for these are located in the System.Windows.Forms assembly.

4- Write the code to listen for mouse clicks

We use the Mouse hook library we added earlier, to detect these. We will listen for mouse double click and mouse down. You can listen for other events the same way.

//When a mouse button is pressed 
           Hook.GlobalEvents().MouseDown += async (sender, e) =>
               Console.WriteLine($"Mouse {e.Button} Down");
           //When a double click is made
           Hook.GlobalEvents().MouseDoubleClick += async (sender, e) =>
               Console.WriteLine($"Mouse {e.Button} button double clicked.");
5- Let’s Add Code to Run The App as a Windows Forms Application.

We will now add code to launch this application as a windows forms app. Without interruption. This is simply done by adding the following line of code.

//Run the app as a windows forma application
Application.Run(new ApplicationContext());


Following these easy 5 steps, permits you to build a simple console app, which detects Mouse clicks and double clicks everywhere on a pc. You may be interested about how to access a PC’s hardware, serial ports and connected devices with C# in THIS post.

office 365 16% reduction on signup.
office 365 16% reduction on signup.

Special Offer

Save 16% on your Microsoft Office 365 subscription        Learn More >>


If you liked this post, or it was useful to you, please 👍 like it, share it on twitter, facebook or other social media… in case you want to get updated on any new useful post, follow me on twitter,  Github  and like my page on facebook. And every other social media by clicking on the  buttons at the end of this post.

Follow me on social media and stay updated


Follow me on social media and stay updated