即使 RegisterHotKey 返回 true,全局热键也不起作用

发布于 2024-10-04 19:55:48 字数 1968 浏览 1 评论 0原文

我需要一个处理全局热键的 activeX: Shift + B

当我从 IE 调用此 ActiveX 时,下面的代码应该执行此操作。

我看到 RegisterHotKey 的结果是 true,这意味着热键已成功注册。

但我没有看到任何消息到达 ThreadPreprocessMessage 方法。为什么?

namespace Kosmala.Michal.ActiveXTest{
    public class ActiveXObject : NativeWindow, IDisposable {
        public const int WM_HOTKEY = 0x0312;
        private IntPtr pFoundWindow ;

        public ActiveXObject(){
            System.Windows.MessageBox.Show("constructor<<");
            Process[] processes = Process.GetProcessesByName("iexplore");
            foreach (Process p in processes){
                pFoundWindow = p.MainWindowHandle;
            }
            System.Windows.MessageBox.Show("pFoundWindow:" + pFoundWindow);
            SetupHotKey(pFoundWindow);
            ComponentDispatcher.ThreadPreprocessMessage += ComponentDispatcher_ThreadPreprocessMessage;
            System.Windows.MessageBox.Show("constructor>>");
        }

        void ComponentDispatcher_ThreadPreprocessMessage(ref MSG msg, ref bool handled){
            System.Windows.MessageBox.Show("inside handler");
            if (msg.message == WM_HOTKEY){
                System.Windows.MessageBox.Show("inside handler");
            }
        }

        private void SetupHotKey(IntPtr handle){
            bool res = RegisterHotKey(handle, GetType().GetHashCode(), 0x0004, 0x42); //Shift + b
            System.Windows.MessageBox.Show("SetupHotKey res:"+res);
        }

        public void Dispose(){
            UnregisterHotKey(_host.Handle, GetType().GetHashCode());
        }

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

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
}

I need an activeX which handles global hotkey: Shift + B

The code below is supposed to do this when I call this ActiveX from IE.

I see the result of RegisterHotKey is true which means that the hotkey has been registered ok.

But I don't see that any messages come to the ThreadPreprocessMessage method. Why?

namespace Kosmala.Michal.ActiveXTest{
    public class ActiveXObject : NativeWindow, IDisposable {
        public const int WM_HOTKEY = 0x0312;
        private IntPtr pFoundWindow ;

        public ActiveXObject(){
            System.Windows.MessageBox.Show("constructor<<");
            Process[] processes = Process.GetProcessesByName("iexplore");
            foreach (Process p in processes){
                pFoundWindow = p.MainWindowHandle;
            }
            System.Windows.MessageBox.Show("pFoundWindow:" + pFoundWindow);
            SetupHotKey(pFoundWindow);
            ComponentDispatcher.ThreadPreprocessMessage += ComponentDispatcher_ThreadPreprocessMessage;
            System.Windows.MessageBox.Show("constructor>>");
        }

        void ComponentDispatcher_ThreadPreprocessMessage(ref MSG msg, ref bool handled){
            System.Windows.MessageBox.Show("inside handler");
            if (msg.message == WM_HOTKEY){
                System.Windows.MessageBox.Show("inside handler");
            }
        }

        private void SetupHotKey(IntPtr handle){
            bool res = RegisterHotKey(handle, GetType().GetHashCode(), 0x0004, 0x42); //Shift + b
            System.Windows.MessageBox.Show("SetupHotKey res:"+res);
        }

        public void Dispose(){
            UnregisterHotKey(_host.Handle, GetType().GetHashCode());
        }

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

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霓裳挽歌倾城醉 2024-10-11 19:55:48

您正尝试在属于另一个进程的窗口中安装热键。 RegisterHotKey() 的文档说:

该函数无法关联热点
带有另一个创建的窗口的键
线程。

它甚至不能与同一个进程中的多个线程一起工作,因此它不可能与其他进程中的线程一起工作。

You're trying to install a hotkey in a window that belongs to another process. The documentation for RegisterHotKey() says:

This function cannot associate a hot
key with a window created by another
thread.

It doesn't even work with multiple threads in the same process, so it can't possibly work with threads from other processes.

风柔一江水 2024-10-11 19:55:48

RegisterHotKey()仅适用于应用程序热键,即特定窗口上的快捷键。全局热键的注册方式不同 - 请参阅热键控件

代码项目上还有一个项目,系统热键,可以在 .NET 中执行此操作。

RegisterHotKey() is only for application hot keys, that is shortcut keys on a specific window. Global hotkeys are registered differently - see Hot Key Controls.

There is also a project on Code Project, System Hot Keys, to do that in .NET.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文