문제

I have a thread executing certain things and right at the end it's supposed to bind my F key as a global hotkey, I've been unable to get this to work, any insight as to what I'm doing wrong OR if RegisterHotKey is not functioning cause of my thread?

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

Edit: Naturally I'm not binding both of these at the same time, one is always commented out.

도움이 되었습니까?

해결책

(Originally a comment, but it does answer the question as posed):

From MSDN, RegisterHotKey:

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

So, simple answer is no.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top