键盘钩子停止使用远程桌面后工作 [英] Keyboard hook stops working after using Remote desktop
本文介绍了键盘钩子停止使用远程桌面后工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下问题:
我的全局键盘钩子停止工作后,我开始任何远程桌面会话,即使会话关闭挂钩不工作所有
My global keyboard hook stops working after i start any Remote Desktop session, even if session is closed hook doesn't work at all.
下面是我的钩
public class KeyboardHooker : IDisposable
{
private IntPtr _hhook;
private static User32.LowLevelKeyboardProc _delegate;
public KeyboardHooker()
{
_delegate = new User32.LowLevelKeyboardProc(HookCallback);
}
public void SetHook()
{
if (IsHookSetted)
return;
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
_hhook = User32.SetWindowsHookEx(User32.WH_KEYBOARD_LL, _delegate, Kernel32.GetModuleHandle(curModule.ModuleName), 0);
}
}
public void ReleaseHook()
{
if (IsHookSetted)
{
User32.UnhookWindowsHookEx(_hhook);
_hhook = IntPtr.Zero;
}
}
public bool IsHookSetted
{
get { return _hhook != IntPtr.Zero; }
}
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
bool handled = false;
switch (wParam.ToInt32())
{
case Messages.WM_SYSKEYDOWN:
case Messages.WM_KEYDOWN:
int vkCode = Marshal.ReadInt32(lParam);
Keys keys = ExtractKey(vkCode);
RaiseKeyDown(keys, ref handled);
break;
case Messages.WM_SYSKEYUP:
case Messages.WM_KEYUP:
vkCode = Marshal.ReadInt32(lParam);
keys = ExtractKey(vkCode);
RaiseKeyUp(keys, ref handled);
break;
}
if (handled)
return IntPtr.Add(IntPtr.Zero, 1);
}
GC.KeepAlive(this);
return User32.CallNextHookEx(_hhook, nCode, wParam, lParam);
}
private Keys ExtractKey(int wParam)
{
Keys key = (Keys)wParam;
return key;
}
#region Events
public event KeyEventHandler KeyDown;
public event KeyEventHandler KeyUp;
private void RaiseKeyUp(Keys keys, ref bool handled)
{
if (KeyUp != null)
{
KeyEventArgs e = new KeyEventArgs(keys);
KeyUp(this, e);
handled = e.Handled;
}
}
private void RaiseKeyDown(Keys keys, ref bool handled)
{
if (KeyDown != null)
{
KeyEventArgs e = new KeyEventArgs(keys);
KeyDown(this, e);
handled = e.Handled;
}
}
#endregion
#region Implementation of IDisposable
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <filterpriority>2</filterpriority>
public void Dispose()
{
ReleaseHook();
}
#endregion
}
请问有人知道如何解决这个问题?
Does anybody know how to solve this problem?
推荐答案
我已经找到了解决办法。它发生在我的代码输入过程键长时间和Windows超脱由超时我的钩。现在,我在处理单独的线程和挂钩每个操作正常工作。
I've found a solution. It happens when my code process keys input for a long time and Windows detached my hook by timeout. Now I process each operation in separate thread and hook works fine.
这篇关于键盘钩子停止使用远程桌面后工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文