Processing Global Windows Mouse and Keyboard Hooks in C#

 

Processing Global Windows Mouse and Keyboard Hooks in C#

By George Mamaladze


This class allows you to tap Window 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.

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.

Using the code

To use this class in your application, you need just to 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());
}

+ Don’t forget to read the comment sections

CodeProject: Processing Global Mouse and Keyboard Hooks in C#. Free source code and programming help

Application-wide Wait Cursor – Automatically Shows when App is busy

 

From a developer’s point of view, using the WaitCursor library could not get any simpler. Add a reference to the WaitCursor assembly and then add the following line to your application start-up code:

ApplicationWaitCursor.Cursor = Cursors.WaitCursor;

That’s it!

You can of course use any Cursor you like, you can use one of the predefined Cursors or you can create a new cursor and use that instead. You can also fine tune the amount of work time that will elapse before the Cursor is shown:

ApplicationWaitCursor.Delay = 
             new TimeSpan(0, 0, 0, 1, 0);  // Delay of 1 second

CodeProject: Automatic Application Wait Cursor. Free source code and programming help

SplitButton: an XP style dropdown split button.

 

image

If you like to have it “Auto Dropdown” whenever you are hover above the Main Button,

try add the following to the original code:

// Overrided this Method to support “Auto Dropdown”
protected override void OnMouseEnter(EventArgs e)
{
    if (_AlwaysDropDown || MouseInSplit())
    {
        if (Enabled)
        {
            SetSplit(_ClickedImage);

            if (this.ContextMenuStrip != null && this.ContextMenuStrip.Items.Count > 0)
            {
                this.ContextMenuStrip.Show(this, new Point(0, Height));
            }
        }
    }
    else
    {
        if (Enabled)
        {
            SetSplit(_NormalImage);
        }
    }

    base.OnMouseEnter(e);
}

 

Via CodeProject: SplitButton: an XP style dropdown split button. Free source code and programming help