更改键被按下用C# [英] Change the key being pressed with C#

查看:114
本文介绍了更改键被按下用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我想C#编写一个程序,将跟踪紧迫的某些键(使用键盘钩子),并发送不同的人来代替。

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.

例如,当我按下A键,它会直接发送Q键。

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

我用的 http://www.codeproject.com/KB/cs/CSLLKeyboardHook.aspx 这对我的挂钩,并试图使用SendKeys功能,但我得到的垃圾异常收集销毁挂钩类里面的一些对象。

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.

推荐答案

首先,你需要挂钩的按键。

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);
    }
}



我已经测试了这个textarea的我'正在写来,当我按下 A 正在发送问:

我不知道会是怎样的魔兽争霸III的行为,也许他们已经封锁,以防止一些僵尸或东西...

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

这篇关于更改键被按下用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆