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.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MouseKeyboardLibrary
{

    /// <summary>
    /// Captures global keyboard events
    /// </summary>
    public class KeyboardHook : GlobalHook
    {

        #region Events

        public event KeyEventHandler KeyDown;
        public event KeyEventHandler KeyUp;
        public event KeyPressEventHandler KeyPress;

        #endregion

        #region Constructor

        public KeyboardHook()
        {

            _hookType = WH_KEYBOARD_LL;

        }

        #endregion

        #region Methods

        protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
        {

            bool handled = false;

            if (nCode > -1 && (KeyDown != null || KeyUp != null || KeyPress != null))
            {

                KeyboardHookStruct keyboardHookStruct =
                    (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

                // Is Control being held down?
                bool control = ((GetKeyState(VK_LCONTROL) & 0x80) != 0) ||
                               ((GetKeyState(VK_RCONTROL) & 0x80) != 0);

                // Is Shift being held down?
                bool shift = ((GetKeyState(VK_LSHIFT) & 0x80) != 0) ||
                             ((GetKeyState(VK_RSHIFT) & 0x80) != 0);

                // Is Alt being held down?
                bool alt = ((GetKeyState(VK_LALT) & 0x80) != 0) ||
                           ((GetKeyState(VK_RALT) & 0x80) != 0);

                // Is CapsLock on?
                bool capslock = (GetKeyState(VK_CAPITAL) != 0);

                // Create event using keycode and control/shift/alt values found above
                KeyEventArgs e = new KeyEventArgs(
                    (Keys)(
                        keyboardHookStruct.vkCode |
                        (control ? (int)Keys.Control : 0) |
                        (shift ? (int)Keys.Shift : 0) |
                        (alt ? (int)Keys.Alt : 0)
                        ));

                // Handle KeyDown and KeyUp events
                switch (wParam)
                {

                    case WM_KEYDOWN:
                    case WM_SYSKEYDOWN:
                        if (KeyDown != null)
                        {
                            KeyDown(this, e);
                            handled = handled || e.Handled;
                        }
                        break;
                    case WM_KEYUP:
                    case WM_SYSKEYUP:
                        if (KeyUp != null)
                        {
                            KeyUp(this, e);
                            handled = handled || e.Handled;
                        }
                        break;

                }

                // Handle KeyPress event
                if (wParam == WM_KEYDOWN &&
                   !handled &&
                   !e.SuppressKeyPress &&
                    KeyPress != null)
                {

                    byte[] keyState = new byte[256];
                    byte[] inBuffer = new byte[2];
                    GetKeyboardState(keyState);

                    if (ToAscii(keyboardHookStruct.vkCode,
                              keyboardHookStruct.scanCode,
                              keyState,
                              inBuffer,
                              keyboardHookStruct.flags) == 1)
                    {

                        char key = (char)inBuffer[0];
                        if ((capslock ^ shift) && Char.IsLetter(key))
                            key = Char.ToUpper(key);
                        KeyPressEventArgs e2 = new KeyPressEventArgs(key);
                        KeyPress(this, e2);
                        handled = handled || e.Handled;

                    }

                }

            }

            if (handled)
            {
                return 1;
            }
            else
            {
                return CallNextHookEx(_handleToHook, nCode, wParam, lParam);
            }

        }

        #endregion

    }

}

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)

About the Author

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

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