2012-06-08 28 views
0

我有一個線程執行某些事情,並在最後它應該綁定我的F鍵作爲全局熱鍵,我一直無法得到這個工作,任何洞察力,我做錯了或如果RegisterHotKey是不是我的線程運行的原因?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); 
} 

編輯:當然,我不是同時綁定這兩個,一個總是被註釋掉。

+0

文檔不只是裝飾。 [RegisterHotKey](http://msdn.microsoft.com/en-us/library/windows/desktop/ms646309(v = vs.85).aspx):「此函數不能將熱鍵與由另一個窗口創建的窗口關聯線。」 –

+0

似乎我錯過了:)謝謝。 –

回答

2

(原文評論,但它確實回答這個問題的提出):

從MSDN,RegisterHotKey

此功能不能與由另一個線程創建一個窗口熱鍵關聯。

所以,簡單的答案是否定的。

相關問題