表单未聚焦时使用多个键

时间:2017-08-01 17:58:36

标签: c# .net visual-studio

我希望在表单没有聚焦时使用多个键来执行不同的操作。 我已经做了什么:当表单没有聚焦时,我只用了一个键来打印出某个文本。

// DLL libraries used to manage hotkeys
[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);

const int F1_HOTKEY_ID = 1;
const int F2_HOTKEY_ID = 1;


public Form1()
{
    InitializeComponent();
    // Modifier keys codes: Alt = 1, Ctrl = 2, Shift = 4, Win = 8
    // Compute the addition of each combination of the keys you want to be pressed
    // ALT+CTRL = 1 + 2 = 3 , CTRL+SHIFT = 2 + 4 = 6...
    RegisterHotKey(this.Handle, F1_HOTKEY_ID, 0, (int)Keys.F1);
    RegisterHotKey(this.Handle, F2_HOTKEY_ID, 0, (int)Keys.F2);
}

private void Form1_Load(object sender, EventArgs e)
{

}

protected override void WndProc(ref Message m)
{
    if (m.Msg == 0x0312 && m.WParam.ToInt32() == F1_HOTKEY_ID)
    {
        SendKeys.Send(txtBoxF1.Text + "{enter}");
    }

    base.WndProc(ref m);
}

现在我想使用多个键,这就是我尝试过的:

protected override void WndProc(ref Message m)
{
    if (m.Msg == 0x0312 && m.WParam.ToInt32() == F1_HOTKEY_ID)
    {
        SendKeys.Send(txtBoxF1.Text + "{enter}");
    }
    if (m.Msg == 0x0312 && m.WParam.ToInt32() == F2_HOTKEY_ID)
    {
        SendKeys.Send(txtBoxF2.Text + "{enter}");
    }
    base.WndProc(ref m);
}

但是当我想要单独打印f2时,它会打印出F1和F2文本。

谢谢。

0 个答案:

没有答案