0

我的应用程序能够检测是否按下了 shift 键,但仅限于第一个修改的键。即使仍然按住该键,第二个修改的键(即使按住 shift 键)也被检测为未修改。

我希望能够正确检测密钥是否在第一个修改的密钥之后被 shift 修改(例如第二个,第三个等等..)

注册代码如下:

[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

public Form1()
    {
        InitializeComponent();
        RegisterHotKey(this.Handle, 1, 0, (int)Keys.A);
        RegisterHotKey(this.Handle, 101, 4, (int)Keys.A);
    }

事件处理程序(按下键时):

protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x0312)
        {
            switch (m.WParam.ToInt32())
            {
                case 1:
                    //add to a textbox
                    keypresstext.AppendText("a");
                    //unregister so the app doesnt rerecord it resulting in a loop
                    UnregisterHotKey(this.Handle, 1);
                    //send the key after intercepting
                    SendKeys.SendWait("a");
                    //reregister
                    RegisterHotKey(this.Handle, 1, 0,(int)Keys.A);
                    break;
                case 101:
                    keypresstext.AppendText("A");
                    UnregisterHotKey(this.Handle, 101);
                    SendKeys.Send("+{a}");
                    RegisterHotKey(this.Handle, 101, 4, (int)Keys.A);
                    break;
             }
        }
        base.WndProc(ref m);
    }
4

0 回答 0