C# WPF全局热键未注册

C# WPF全局热键未注册,c#,wpf,C#,Wpf,我有一个代码,可以在我的应用程序中按下Shift+Numpad1时进行检测。热键已成功注册,但WndProc函数中的if语句从未触发(即“热键按下”消息框从未显示)。相关代码附于下文 private GlobalHotkey ghk; public MainWindow() { InitializeComponent(); ghk = new GlobalHotkey(Constants.SHIF

我有一个代码,可以在我的应用程序中按下Shift+Numpad1时进行检测。热键已成功注册,但WndProc函数中的if语句从未触发(即“热键按下”消息框从未显示)。相关代码附于下文

        private GlobalHotkey ghk;

        public MainWindow()
        {
            InitializeComponent();

            ghk = new GlobalHotkey(Constants.SHIFT, Keys.NumPad1, new WindowInteropHelper(System.Windows.Application.Current.MainWindow).Handle);
            if (!ghk.Register())
            {
                MessageBox.Show("Hotkey Failed to register");
            }
            else
            {
                MessageBox.Show("Hotkey Registered");
            }
            

            NotifyIcon icon = new NotifyIcon();
            icon.Icon = new Icon("MainIcon.ico");
            icon.Visible = true;
            icon.DoubleClick += delegate (object sender, EventArgs args) {
                Activate();
                WindowState = WindowState.Normal;
            };

            icon.ContextMenu = new System.Windows.Forms.ContextMenu(new MenuItem[] { 
                new MenuItem("Quit", (object s, EventArgs e) => {
                        Close();
                    }) 
            });
            
        }

        protected override void OnStateChanged(EventArgs e)
        {
            if (WindowState == WindowState.Minimized)
                Hide();

            base.OnStateChanged(e);
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
            source.AddHook(WndProc);
        }

        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            textBox.Text = msg.ToString();
            if (msg == Constants.WM_HOTKEY_MSG_ID)
            {
                HandleHotkey();
            }

            return IntPtr.Zero;
        }

        private void HandleHotkey()
        {
            MessageBox.Show("HotkeyPressed");
            Activate();
        }

在代码
常量中。WM\u热键\u MSG\u ID
=0x0312

全局热键类:

public class GlobalHotkey
    {
        private int modifier;
        private int key;
        private IntPtr hWnd;
        private int id;

        public GlobalHotkey(int modifier, Keys key, IntPtr handle)
        {
            this.modifier = modifier;
            this.key = (int)key;
            this.hWnd = handle;
            this.id = this.GetHashCode();
        }

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

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

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

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

你试过调试吗?如果(msg==Constants.WM\u HOTKEY\u msg\u ID)并查看发生了什么,消息正在发送,但即使我按了正确的组合键,它们也不等于0x0312。当按下两个按钮时,您正在尝试激活一个东西。这不是通常的方式。每个按钮都存在事件。不适用于按钮的组合。可能会触发两个按钮的两个不同事件。如果我未应用任何修改器(因此仅按下Numpad 1),则代码也不起作用。您注册热键太早。在
OnSourceInitialized
(在
base.OnSourceInitialized(e);
之后)而不是在窗口构造函数中执行此操作。您是否正在引用System.Windows.Forms(键<代码>内容)?