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

.NET Framework Library Source Code available for viewing

 

It’s live and you can give it a try now! Ten minutes ago Shawn and Scott released the hounds. If you’d like to step through .NET Framework Source code, here’s what you need to do.

  1. Install this QFE.
  • Note, if you’re on 64-bit Windows, read the description as there is a single manual step for 64-bit folks like me.
  • Go into Tools|Options|Debugging|General and turn off “Enable Just My Code” and turn on “Enable Source Server.”
  • Go to Symbols and add this URL http://referencesource.microsoft.com/symbols and a local cache path. Make sure “search only when symbols are loaded manually” is checked.

     

  • Via .NET Framework Library Source Code available for viewing

    Parallel Extensions to .NET Framework 3.5 screencast from Daniel Moth

    image

    1. Tour of the Samples. A tour of what gets installed to get you started.
    2. Declarative data parallelism. This is about PLINQ.
    3. Imperative data parallelism. This is about the static Parallel class

    Posted in .NET. No Comments »

    Parallel Extensions to the .NET FX CTP

     

    Parallel Extensions to the .NET Framework technology, available for download on MSDN.  This release contains new APIs to make programming on the .NET Framework simpler as well as supporting documentation and samples. 

    Parallel Extensions runs on .NET FX 3.5, and relies on features available in C# 3.0 and VB 9.0 and includes:

    • Imperative data and task parallelism APIs, including parallel for and foreach loops, to make the transition from sequential to parallel programs simpler.
    • Declarative data parallelism in the form of a data parallel implementation of LINQ-to-Objects.  This allows you to run LINQ queries on multiple processors.
    • First class tasks that can be used to schedule, wait on, and cancel parallel work.
    • New concurrency runtime used across the library to enable lightweight tasks and effectively map and balance the concurrency expressed in code to available concurrent resources on the execution platform.
    • Several great examples of how to use parallelism in real world problems to obtain impressive speedups, including a raytracer, Sudoku puzzle generator, and other simple puzzle solvers and smaller samples.

    Links:

    Posted in .NET. No Comments »