注册热键是否在单独的线程中不起作用

本文关键字:线程 不起作用 单独 是否 注册 | 更新日期: 2023-09-27 18:36:31

我有一个线程执行某些事情,最后它应该将我的 F 键绑定为全局热键,我一直无法让它工作,关于我做错了什么的任何见解,或者如果 RegisterHotKey 无法正常工作导致我的线程?

[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, System.Windows.Input.ModifierKeys fsModifiers, Keys vk);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private const int WmHotKey = 0x0312;
private void onLoad()
{
   //RegisterHotKey(this.Handle, 0, System.Windows.Input.ModifierKeys.None, Keys.F); // This works
}
private void OnSeparateThread()
{
   // This gets called by a separate thread, in the full version of the code more stuff
   // happen here, which does get executed.
   RegisterHotKey(this.Handle, 0, System.Windows.Input.ModifierKeys.None, Keys.F); // Does not bind
}
protected override void WndProc(ref Message m)
{
   if (m.Msg == WmHotKey)
   {
      MessageBox.Show("Test me!");
   }
   base.WndProc( ref m );
}

编辑:当然,我不会同时绑定这两个,一个总是被注释掉。

注册热键是否在单独的线程中不起作用

(最初是一个评论,但它确实回答了提出的问题):

从MSDN,RegisterHotKey:

此函数无法将热键与其他线程创建的窗口相关联。

所以,简单的答案是否定的。