Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Global Mouse and Keyboard Library

, 8 Aug 2008
Easy-to-use library with global mouse and keyboard hooks and simulators.
MouseKeyboardLib2003.zip
MouseKeyboardLibrary11
GlobalMacroRecorder
App.ico
bin
Debug
GlobalMacroRecorder.exe
MouseKeyboardLibrary.dll
GlobalMacroRecorder.csproj.user
MouseKeyboardLibrary
bin
Debug
MouseKeyboardLibrary.dll
MouseKeyboardLibrary.csproj.user
SampleApplication
App.ico
bin
Debug
MouseKeyboardLibrary.dll
SampleApplication.exe
SampleApplication.csproj.user
MouseKeyboardLib2005.zip
MouseKeyboardLibrary20
GlobalMacroRecorder
bin
Debug
GlobalMacroRecorder.exe
GlobalMacroRecorder.vshost.exe
MouseKeyboardLibrary.dll
Properties
Settings.settings
MouseKeyboardLibrary
bin
Debug
MouseKeyboardLibrary.dll
Properties
SampleApplication
bin
Debug
MouseKeyboardLibrary.dll
SampleApplication.exe
SampleApplication.vshost.exe
Properties
Settings.settings
MouseKeyboardLib2008.zip
MouseKeyboardLibrary
GlobalMacroRecorder
bin
Debug
GlobalMacroRecorder.exe
GlobalMacroRecorder.vshost.exe
GlobalMacroRecorder.vshost.exe.manifest
MouseKeyboardLibrary.dll
Properties
Settings.settings
MouseKeyboardLibrary
bin
Debug
MouseKeyboardLibrary.dll
Properties
SampleApplication
bin
Debug
MouseKeyboardLibrary.dll
SampleApplication.exe
SampleApplication.vshost.exe
SampleApplication.vshost.exe.manifest
Properties
Settings.settings
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using MouseKeyboardLibrary;

namespace SampleApplication
{
    public partial class HookTestForm : Form
    {

        MouseHook mouseHook = new MouseHook();
        KeyboardHook keyboardHook = new KeyboardHook();

        public HookTestForm()
        {
            InitializeComponent();
        }

        private void TestForm_Load(object sender, EventArgs e)
        {

            mouseHook.MouseMove += new MouseEventHandler(mouseHook_MouseMove);
            mouseHook.MouseDown += new MouseEventHandler(mouseHook_MouseDown);
            mouseHook.MouseUp += new MouseEventHandler(mouseHook_MouseUp);
            mouseHook.MouseWheel += new MouseEventHandler(mouseHook_MouseWheel);

            keyboardHook.KeyDown += new KeyEventHandler(keyboardHook_KeyDown);
            keyboardHook.KeyUp += new KeyEventHandler(keyboardHook_KeyUp);
            keyboardHook.KeyPress += new KeyPressEventHandler(keyboardHook_KeyPress);

            mouseHook.Start();
            keyboardHook.Start();

            SetXYLabel(MouseSimulator.X, MouseSimulator.Y);

        }

        void keyboardHook_KeyPress(object sender, KeyPressEventArgs e)
        {

            AddKeyboardEvent(
                "KeyPress",
                "",
                e.KeyChar.ToString(),
                "",
                "",
                ""
                );

        }

        void keyboardHook_KeyUp(object sender, KeyEventArgs e)
        {

            AddKeyboardEvent(
                "KeyUp",
                e.KeyCode.ToString(),
                "",
                e.Shift.ToString(),
                e.Alt.ToString(),
                e.Control.ToString()
                );

        }

        void keyboardHook_KeyDown(object sender, KeyEventArgs e)
        {


            AddKeyboardEvent(
                "KeyDown",
                e.KeyCode.ToString(),
                "",
                e.Shift.ToString(),
                e.Alt.ToString(),
                e.Control.ToString()
                );

        }

        void mouseHook_MouseWheel(object sender, MouseEventArgs e)
        {

            AddMouseEvent(
                "MouseWheel",
                "",
                "",
                "",
                e.Delta.ToString()
                );

        }

        void mouseHook_MouseUp(object sender, MouseEventArgs e)
        {


            AddMouseEvent(
                "MouseUp",
                e.Button.ToString(),
                e.X.ToString(),
                e.Y.ToString(),
                ""
                );

        }

        void mouseHook_MouseDown(object sender, MouseEventArgs e)
        {


            AddMouseEvent(
                "MouseDown",
                e.Button.ToString(),
                e.X.ToString(),
                e.Y.ToString(),
                ""
                );


        }

        void mouseHook_MouseMove(object sender, MouseEventArgs e)
        {

            SetXYLabel(e.X, e.Y);

        }

        void SetXYLabel(int x, int y)
        {

            curXYLabel.Text = String.Format("Current Mouse Point: X={0}, y={1}", x, y);

        }

        void AddMouseEvent(string eventType, string button, string x, string y, string delta)
        {

            listView1.Items.Insert(0,
                new ListViewItem(
                    new string[]{
                        eventType, 
                        button,
                        x,
                        y,
                        delta
                    }));

        }

        void AddKeyboardEvent(string eventType, string keyCode, string keyChar, string shift, string alt, string control)
        {

            listView2.Items.Insert(0,
                 new ListViewItem(
                     new string[]{
                        eventType, 
                        keyCode,
                        keyChar,
                        shift,
                        alt,
                        control
                }));

        }

        private void TestForm_FormClosed(object sender, FormClosedEventArgs e)
        {

            mouseHook.Stop();
            keyboardHook.Stop();

        }


    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Brian Geiman
Software Developer GEA Refrigeration
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 8 Aug 2008
Article Copyright 2008 by Brian Geiman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid