KeyHook 用于检测同时按下的多个键

Posted

技术标签:

【中文标题】KeyHook 用于检测同时按下的多个键【英文标题】:KeyHook used to detect multiple keys pressed at the same time 【发布时间】:2019-05-17 15:04:10 【问题描述】:

您好,我已经创建了一个程序来监控键盘,如果按下 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();
    

我已经测试了钩子,如果我只检查是否按下了一个键,它就可以正常工作,但如果我想检查是否按下了多个键,则它不起作用。

【问题讨论】:

嗯......每个键都会引发中断,因此您需要跟踪按下了哪些键或查看这些键的状态 我将如何查找这些州? 【参考方案1】:

尝试使用

if(Keyboard.IsKeyDown(Key.LeftShift) && Keyboard.IsKeyDown(Key.LeftCtrl) && Keyboard.IsKeyDown(Key.R)) //do something 

也许对你有帮助

Capture multiple key downs in C#

【讨论】:

以上是关于KeyHook 用于检测同时按下的多个键的主要内容,如果未能解决你的问题,请参考以下文章

检测 OS X 上的任何按键,包括修改键

检测修饰键按下?

检测功能键被按下的最佳方法是啥?

检测按下的键何时是第一个字符

如何检测键盘上最后按下的箭头键

检测使用javascript按下的输入键[重复]