c#注册没有挂钩库的全局热键(键盘挂钩)

时间:2013-07-17 13:59:09

标签: c# winforms keyboard hotkeys

我尝试在C#WinForm应用程序中配置一些全局热键,这些应用程序可以使用和不使用焦点。为此,我在一些Hooking Librarys中扮演了一些角色,如:Link

但这些图书馆的工作效率并不高,所以我决定采用不同的方式。我找到了一个,但有一个问题:使用下面发布的新方法我可以捕获按键,但我也想捕获keydown(WM_KEYDOWN / 0x100)和keyup事件......但这不起作用。有什么想法吗?

代码:

public Form1()
        {
            InitializeComponent();
        }

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



            const int MOD_CONTROL = 0x0002;
            const int MOD_SHIFT = 0x0004;
            const int WM_HOTKEY = 0x0312;
            const int WM_KEYDOWN = 0x0100;

private void Form1_Load(object sender, EventArgs e)
        {

            // Hook Keys
            RegisterHotKey(this.Handle, 1, MOD_CONTROL, (int)0);
            RegisterHotKey(this.Handle, 2, MOD_CONTROL, (int)Keys.X);

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            UnregisterHotKey(this.Handle, 1);
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_HOTKEY && (int)m.WParam == 1)
            {
                MessageBox.Show("here");
            }
            if (m.Msg == WM_KEYDOWN && (int)m.WParam == 2)
                this.Opacity = 100;
            base.WndProc(ref m);
        }

0 个答案:

没有答案
相关问题