我有一个线程执行某些事情,在最后,它应该绑定我的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 );
}编辑:当然,我不会同时绑定这两个,其中一个总是被注释掉。
发布于 2012-06-08 22:31:57
(最初是一个评论,但它确实回答了提出的问题):
来自MSDN,RegisterHotKey
此函数不能将热键与另一个线程创建的窗口相关联。
所以,简单的答案是不。
https://stackoverflow.com/questions/10950530
复制相似问题