我发现这件小件可以注册一个热键:

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

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x0312)
            MessageBox.Show("Hotkey pressed");
        base.WndProc(ref m);
    }

    public FormMain()
    {
        InitializeComponent();
        //Alt + A
        RegisterHotKey(this.Handle, this.GetType().GetHashCode(), 1, (int)'A');
    }

它可以很好地工作,但是我的问题是我想使用两个不同的快捷方式。我知道第二个参数是ID,因此我认为我可以制作不同的ID并在WNDProc函数中添加新的IF语句,但我不确定该如何处理。

简而言之,我将如何创建第二个快捷方式?

谢谢,

有帮助吗?

解决方案

 RegisterHotKey(this.Handle, this.GetType().GetHashCode(), 1, (int)'A')

请勿在此处使用GethashCode()。只需编号您的热键,从0开始。没有任何混合ID的危险,热键ID对于每个手柄都是特定的。你会得到的 ID 返回WNDPROC()方法。使用m.wparam.toint32()获取值:

protected override void WndProc(ref Message m)
{
    if (m.Msg == 0x0312) {    // Trap WM_HOTKEY
        int id = m.WParam.ToInt32();
        MessageBox.Show(string.Format("Hotkey #{0} pressed", id));
    }
    base.WndProc(ref m);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top