Click here to Skip to main content
12,353,277 members (69,578 online)
Click here to Skip to main content

Stats

280.3K views
19.2K downloads
309 bookmarked
Posted

Global Mouse and Keyboard Library

, 8 Aug 2008 CPOL
Easy-to-use library with global mouse and keyboard hooks and simulators.
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
MouseKeyboardLibrary20
GlobalMacroRecorder
bin
Debug
GlobalMacroRecorder.exe
GlobalMacroRecorder.vshost.exe
MouseKeyboardLibrary.dll
Properties
MouseKeyboardLibrary
bin
Debug
MouseKeyboardLibrary.dll
Properties
SampleApplication
bin
Debug
MouseKeyboardLibrary.dll
SampleApplication.exe
SampleApplication.vshost.exe
Properties
MouseKeyboardLibrary
GlobalMacroRecorder
bin
Debug
GlobalMacroRecorder.exe
GlobalMacroRecorder.vshost.exe
GlobalMacroRecorder.vshost.exe.manifest
MouseKeyboardLibrary.dll
Properties
MouseKeyboardLibrary
bin
Debug
MouseKeyboardLibrary.dll
Properties
SampleApplication
bin
Debug
MouseKeyboardLibrary.dll
SampleApplication.exe
SampleApplication.vshost.exe
SampleApplication.vshost.exe.manifest
Properties
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace MouseKeyboardLibrary
{

    /// <summary>
    /// Standard Keyboard Shortcuts used by most applications
    /// </summary>
    public enum StandardShortcut
    {
        Copy,
        Cut,
        Paste,
        SelectAll,
        Save,
        Open,
        New,
        Close,
        Print
    }

    /// <summary>
    /// Simulate keyboard key presses
    /// </summary>
    public static class KeyboardSimulator
    {

        #region Windows API Code

        const int KEYEVENTF_EXTENDEDKEY = 0x1;
        const int KEYEVENTF_KEYUP = 0x2;

        [DllImport("user32.dll")]
        static extern void keybd_event(byte key, byte scan, int flags, int extraInfo); 

        #endregion

        #region Methods

        public static void KeyDown(Keys key)
        {
            keybd_event(ParseKey(key), 0, 0, 0);
        }

        public static void KeyUp(Keys key)
        {
            keybd_event(ParseKey(key), 0, KEYEVENTF_KEYUP, 0);
        }

        public static void KeyPress(Keys key)
        {
            KeyDown(key);
            KeyUp(key);
        }

        public static void SimulateStandardShortcut(StandardShortcut shortcut)
        {
            switch (shortcut)
            {
                case StandardShortcut.Copy:
                    KeyDown(Keys.Control);
                    KeyPress(Keys.C);
                    KeyUp(Keys.Control);
                    break;
                case StandardShortcut.Cut:
                    KeyDown(Keys.Control);
                    KeyPress(Keys.X);
                    KeyUp(Keys.Control);
                    break;
                case StandardShortcut.Paste:
                    KeyDown(Keys.Control);
                    KeyPress(Keys.V);
                    KeyUp(Keys.Control);
                    break;
                case StandardShortcut.SelectAll:
                    KeyDown(Keys.Control);
                    KeyPress(Keys.A);
                    KeyUp(Keys.Control);
                    break;
                case StandardShortcut.Save:
                    KeyDown(Keys.Control);
                    KeyPress(Keys.S);
                    KeyUp(Keys.Control);
                    break;
                case StandardShortcut.Open:
                    KeyDown(Keys.Control);
                    KeyPress(Keys.O);
                    KeyUp(Keys.Control);
                    break;
                case StandardShortcut.New:
                    KeyDown(Keys.Control);
                    KeyPress(Keys.N);
                    KeyUp(Keys.Control);
                    break;
                case StandardShortcut.Close:
                    KeyDown(Keys.Alt);
                    KeyPress(Keys.F4);
                    KeyUp(Keys.Alt);
                    break;
                case StandardShortcut.Print:
                    KeyDown(Keys.Control);
                    KeyPress(Keys.P);
                    KeyUp(Keys.Control);
                    break;
            }
        }

        static byte ParseKey(Keys key)
        {

            // Alt, Shift, and Control need to be changed for API function to work with them
            switch (key)
            {
                case Keys.Alt:
                    return (byte)18;
                case Keys.Control:
                    return (byte)17;
                case Keys.Shift:
                    return (byte)16;
                default:
                    return (byte)key;
            }

        } 

        #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)

Share

About the Author

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

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160621.1 | Last Updated 8 Aug 2008
Article Copyright 2008 by Brian Geiman
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid