嗨,我创建了一个程序来监控键盘,如果按下 CTRL+Shift+R,程序必须开始记录键并将它们写入文本文件。
我的第二部分(监控键和写入文本文件)工作得很好,不幸的是我的系统无法检测到是否同时按下了 ctrl+Shift+R。我将展示我正在使用的课程以及问题代码
我的班级我正在使用钥匙钩
public class LowLevelKeyboardListener
{
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private const int WM_SYSKEYDOWN = 0x0104;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
public delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
public event EventHandler<KeyPressedArgs> OnKeyPressed;
private LowLevelKeyboardProc _proc;
private IntPtr _hookID = IntPtr.Zero;
public LowLevelKeyboardListener()
{
_proc = HookCallback;
}
public void HookKeyboard()
{
_hookID = SetHook(_proc);
}
public void UnHookKeyboard()
{
UnhookWindowsHookEx(_hookID);
}
private IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
}
}
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
if (OnKeyPressed != null) { OnKeyPressed(this, new KeyPressedArgs(KeyInterop.KeyFromVirtualKey(vkCode))); }
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
}
public class KeyPressedArgs : EventArgs
{
public Key KeyPressed { get; private set; }
public KeyPressedArgs(Key key)
{
KeyPressed = key;
}
}
我的问题代码没有被检测到
public void _listener_OnKeyPressed(Object sender, KeyPressedArgs e)
{
if (e.KeyPressed == Key.LeftCtrl && e.KeyPressed == Key.LeftShift && e.KeyPressed == Key.R)
{
if (recording)
{
System.Windows.Forms.MessageBox.Show("Recording");
recording = false;
}
else
{
System.Windows.Forms.MessageBox.Show("Recording Halted");
recording = true;
}
}
我正在使用的其他一些代码,但可以 100% 工作
private void Window_Loaded(object sender, RoutedEventArgs e)
{
_listener = new LowLevelKeyboardListener();
_listener.OnKeyPressed += _listener_OnKeyPressed;
_listener.HookKeyboard();
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.ShowDialog();
pathName = dlg.FileName;
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
_listener.UnHookKeyboard();
}
我已经测试了钩子,如果我只检查是否按下了一个键,它就可以正常工作,但如果我想检查是否按下了多个键,它就不起作用。