如何同时为(LETTER)和SHIFT +(LETTER)注册热键

时间:2015-12-11 01:13:48

标签: c# keyboard hotkeys

我正在用C#构建一个应用程序来更改某些键的键盘输出 使用globl热键 当我为一个键注册一个热键,然后为该键+修饰符注册另一个热键时,只有最后一个键有效 例如 如果我做

RegisterHotKey(MyForm.Handle, 100, 0, (int)Keys.T); // T
RegisterHotKey(MyForm.Handle, 200, 4, (int)Keys.T); // Shift + T

它只捕获Shift + T按下。

当我做的时候

RegisterHotKey(MyForm.Handle, 100, 4, (int)Keys.T); // Shift + T
RegisterHotKey(MyForm.Handle, 200, 0, (int)Keys.T); // T

它仅捕获“T”按

有办法处理这两种情况吗?

更新

这是我的代码

public partial class Form1 : Form
{
    [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);

    private KeyHandler ghk;

    public Dictionary<Keys, string> ckeys = new Dictionary<Keys, string>();

    public Form1()
    {
        InitializeComponent();
    }  

    private void Form1_Load(object sender, EventArgs e)
    {
        registerApp();
    }  


    private void registerKey(Keys k, string o, int m = 0)
    {
        ghk = new KeyHandler(k, this, o, m);
        ghk.Register();
    }

    private void HandleHotkey(Keys id)
    {
        SendKeys.Send(ckeys[id]);
    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == Constants.WM_HOTKEY_MSG_ID)
        {
            int id = m.WParam.ToInt32();
            HandleHotkey((Keys)id);
        }

        base.WndProc(ref m);
    }


    private void registerApp()
    {
        registerKey(Keys.T, "ت");
        registerKey(Keys.T, "ط", KeyModifier.Shift);
    }


    //Modifiers
    static class KeyModifier
    {
        public static int None = 0;
        public static int Alt = 1;
        public static int Control = 2;
        public static int Shift = 4;
        public static int WinKey = 8;
    }

}


//Class KeyHandler
public class KeyHandler
{
    [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);

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

    public KeyHandler(Keys key, Form1 form, string output, int modifier)
    {
        this.key = (int)key;
        this.modifier = modifier;
        this.hWnd = form.Handle;
        id = this.key;
        form.ckeys[key] = output;
    }

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

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

    public bool Unregiser()
    {
        return UnregisterHotKey(hWnd, id);
    }
}

//Constants
public static class Constants
{
    //windows message id for hotkey
    public const int WM_HOTKEY_MSG_ID = 0x0312;
}

1 个答案:

答案 0 :(得分:-1)

错误是T和Shift + T注册了相同的id 我通过带有不同id的registring Shift + T解决了这个问题。

相关问题