键盘钩存储键

时间:2017-02-27 11:27:57

标签: c# wpf keystroke keyboard-hook

我已经实现了this回答中提供的键盘钩子。除了一个案例,一切正常。如果我想按住键一段时间,从而在技术上重新使用我正在调用的方法,有两件事情发生:

  1. 我留在方法(这很好)
  2. 应用程序记录按下的键,作为独立的击键,然后由钩子捕获。
  3. 胡克:

    private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
    
            const int WM_HOTKEY = 0x0312;
                switch (msg)
                {
                    case WM_HOTKEY:
                        if (battleBotStatus == 0)
                        {
                            switch (wParam.ToInt32())
                            {
    
                                case HOTKEY_ID:
                                    OnHotKeyPressed();
                                    handled = true;
                                    break;
    
                                case HOTKEY_ID9:
                                    enableBattleBotKeys();
                                    battleBotStatus = 1;
                                    //performCombo();
                                    handled = true;
                                    break;
                            }
                        }
                        else
                        {
                            switch (wParam.ToInt32())
                            {
    
                                case BattleKey1:
                                    performCombo(1);
                                    handled = true;                                    
                                    buttonIsPressed = false;
                                    break;
                            }
                        }
                        break;
                }
            return IntPtr.Zero;
        }
    

    PerformCombo代码

    private void performCombo(int n)
            {
                string simpleAction;
                Sleep(cmbDelay);
                Key k = new Key();
                switch (n) 
                {
                    case 1:
                        k = Key.D1;
                        break;
                    case 2:
                        k = Key.D2;
                        break;
                    case 3:
                        k = Key.D3;
                        break;
                    case 4:
                        k = Key.D4;
                        break;
                    case 5:
                        k = Key.D5;
                        break;
                    case 6:
                        k = Key.D6;
                        break;
                }
            if ((Keyboard.GetKeyStates(k) & KeyStates.Down) > 0)
                Sleep(1000);
            do
            {
                if (comboList[n - 1].Count > 0)
                {
                    foreach (string action in comboList[n - 1])
                    {
                        simpleAction = action.Split('~')[0];
                        if (simpleAction.Length == 1)
                        {
                            dkeyinput(simpleAction, gamePid, 500);
                            Sleep(cmbDelay);
                        }
                        else
                        {
                            if (simpleAction == "LeftClick")
                            {
                                dmouseclick("left", 300);
                                Sleep(cmbDelay);
                            }
                            else if (simpleAction == "RightClick")
                            {
                                dmouseclick("right", 300);
                                Sleep(cmbDelay);
                            }
                            else if (simpleAction == "Space")
                            {
                                dkeyinput("\"{SPACE}\"", gamePid);
                                Sleep(cmbDelay);
                            }
                            else if (simpleAction.Contains("&"))
                            {
                                dkeyinput2(simpleAction, cmbDelay);
                                Sleep(cmbDelay);
                            }
                        }
                    }
                }
            }
            while ((Keyboard.GetKeyStates(k) & KeyStates.Down) > 0);
    
    
        }  
    

    我曾试图使用旗帜,但它们似乎也没有用。在检查使用调试器时,我注意到它经常进入处理程序并继续执行BattleKey1案例的操作,即使在释放密钥后也是如此。

1 个答案:

答案 0 :(得分:0)

作为一种快速解决方法,如果你知道最大组合时间: 为DateTime添加新的do-while变量:

var comboStart = DateTime.Now;
do{...}while

do-while添加时间检查:

while ((Keyboard.GetKeyStates(k) & KeyStates.Down) > 0 ||
(DateTime.Now - comboStart).TotalSeconds > maxComboTimeInSeconds);

这不会删除应用程序freezes(您需要在Async中运行此方法才能这样做),但如果您按某个键,则会为do-while打破它很长一段时间。