tags:

views:

140

answers:

2

Hey, I'm trying to write a program in C# that will track the pressing of certain keys (using a keyboard hook), and send different ones instead.

For instance, when I press the A key it will instead send the Q key.

I used http://www.codeproject.com/KB/cs/CSLLKeyboardHook.aspx this for my hooks and tried to use the SendKeys function, but I get an exception about the garbage collector destroying some object inside the hook class.

+1  A: 

and when you look at your hook class what is the source of the problem? Sounds like a resource not being managed properly.

Realize that if you are planning on doing this as some sort of practical joke these never go over well because generally of the inability to turn these off. Also recognize that this type of seemingly unethical topic will not likely get much support.

Driss Zouak
+1 for the warning regarding deactivation difficulty.
Val
Also I thought I might chip in that this sort of thing does have legitimate uses, such as switching keyboard layouts. Eg: QWERTY to DVORAK
Val
+1  A: 

First you need to hook up the keys.

With this class you can register a global shortcut, I'm skipping the explanation, but you can read it here.

public class KeyboardHook
{
    [DllImport("user32.dll")]
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);

    [DllImport("user32.dll")]
    private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    public enum Modifiers
    {
        None = 0x0000,
        Alt = 0x0001,
        Control = 0x0002,
        Shift = 0x0004,
        Win = 0x0008
    }

    int modifier;
    int key;
    IntPtr hWnd;
    int id;

    public KeyboardHook(int modifiers, Keys key, Form f)
    {
        this.modifier = modifiers;
        this.key = (int)key;
        this.hWnd = f.Handle;
        id = this.GetHashCode();
    }

    public override int GetHashCode()
    {
        return modifier ^ key ^ hWnd.ToInt32();
    }


    public bool Register()
    {
        return RegisterHotKey(hWnd, id, modifier, key);
    }
    public bool Unregister()
    {
        return UnregisterHotKey(hWnd, id);
    }
}

Then on your form you have to register the shortcut

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        KeyboardHook hook = new KeyboardHook((int)KeyboardHook.Modifiers.None, Keys.A, this);

        hook.Register(); // registering globally that A will call a method
    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x0312)
            HandleHotkey(); // A, which was registered before, was pressed
        base.WndProc(ref m);
    }

    private void HandleHotkey()
    {
        // instead of A send Q
        KeyboardManager.PressKey(Keys.Q);
    }
}

And here the class to manage Keyboard press and release events.

public class KeyboardManager
{
    public const int INPUT_KEYBOARD = 1;
    public const int KEYEVENTF_KEYUP = 0x0002;

    public struct KEYDBINPUT
    {
        public Int16 wVk;
        public Int16 wScan;
        public Int32 dwFlags;
        public Int32 time;
        public Int32 dwExtraInfo;
        public Int32 __filler1;
        public Int32 __filler2;
    }

    public struct INPUT
    {
        public Int32 type;
        public KEYDBINPUT ki;
    }

    [DllImport("user32")]
    public static extern int SendInput(int cInputs, ref INPUT pInputs, int cbSize);

    public static void HoldKey(Keys vk)
    {
        INPUT input = new INPUT();
        input.type = INPUT_KEYBOARD;
        input.ki.dwFlags = 0;
        input.ki.wVk = (Int16)vk;
        SendInput(1, ref input, Marshal.SizeOf(input));
    }

    public static void ReleaseKey(Keys vk)
    {
        INPUT input = new INPUT();
        input.type = INPUT_KEYBOARD;
        input.ki.dwFlags = KEYEVENTF_KEYUP;
        input.ki.wVk = (Int16)vk;
        SendInput(1, ref input, Marshal.SizeOf(input));
    }

    public static void PressKey(Keys vk)
    {
        HoldKey(vk);
        ReleaseKey(vk);
    }
}

I've tested it on this textarea which I'm writing to, when I pressed A it was sending Q.

I'm not sure what will be the behavior on Warcraft III, maybe they have blocked to prevent some kind of bot or something...

BrunoLM