即使 RegisterHotKey 返回 true,全局热键也不起作用
我需要一个处理全局热键的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正尝试在属于另一个进程的窗口中安装热键。 RegisterHotKey() 的文档说:
它甚至不能与同一个进程中的多个线程一起工作,因此它不可能与其他进程中的线程一起工作。
You're trying to install a hotkey in a window that belongs to another process. The documentation for RegisterHotKey() says:
It doesn't even work with multiple threads in the same process, so it can't possibly work with threads from other processes.
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.