分别发送keydown和keyup

时间:2018-09-05 01:52:37

标签: c# sendkeys

我当前正在使用此代码:

else if (y2 > 0 && x2 < 0) {
  SendKeys.SendWait("wa");
  completion = completion + 0.1;
  System.Threading.Thread.Sleep(2);
  if (completion > y2) {
    break;
  }
}

但是,我不想做SendKeys.SendWait,而是只按下这些按钮,而不是按下downupdownup。

1 个答案:

答案 0 :(得分:1)

从这个SendMessage, When To Use KEYDOWN, SYSKEYDOWN, etc?的Stackoverflow问题看来,发送按键消息的方法似乎需要使用user32.dll库:

boolean firstInput = false;
        while(firstInput == false)
        {
            System.out.println("1st complex number:");
            try 
            {
            numberA = getComplex();
            firstInput = true;
            } catch (InputMismatchException e) {
                System.out.println("Your input is invalid. Please try again.");
                firstInput = false;
            }
        }


public static Complex getComplex() throws InputMismatchException
    {
        Scanner keyboard = new Scanner(System.in);
        double[] tempComplexNumbers = new double[2];

        for (int i = 0; i < tempComplexNumbers.length; i++)
        {
            if(i == 0)
            {
                System.out.println("Please enter the real part of your complex number.");
            }
            else if(i == 1)
            {
                System.out.println("Please enter the imaginary part of your complex number.");
            }
            tempComplexNumbers[i] = keyboard.nextDouble();
        }

        return new Complex(tempComplexNumbers[0], tempComplexNumbers[1]);
    }

参数[DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam); 具有允许发送KeyDown事件的选项:

Msg

如果您可以灵活地在.NET之外的其他环境中实施解决方案,则AutoHotkey提供了一组丰富的功能来发送输入。