【问题标题】:problem with keyboard simulate press event键盘模拟按下事件的问题
【发布时间】:2009-11-23 17:29:04
【问题描述】:

我正在尝试制作一个自动化工具,用于在工作中为调度程序应用程序填充用户名的文本框。

如果 inputArrayX[i] 数组包含 a,b,c,我在尝试模拟按键时遇到了一些问题

keyboardsim 会按 abc,但如果数组包含 a、b、b、c、c,它仍然只输入 abc,而不是像我想要的那样输入 abbcc。

有人知道我在这里做错了什么吗?

    private void MouseMacroChangeUser()
    {

        //move form to 0,0
        this.Location = new Point(0, 0);
        //set xy to mouse current pos
        userMousePos();
        //inputBlocker();
        xX = int.Parse(this.Location.X.ToString());
        yY = int.Parse(this.Location.Y.ToString());
        defaultMousePos();
        //Thread.Sleep(600);

        Cursor.Position = new Point(Cursor.Position.X + 739, Cursor.Position.Y + 162);
        //Thread.Sleep(100);
        MouseSimulator.DoubleClick(MouseButton.Left);
        for (int i = 0; i < inputArrayX.Length; i++)
        {
            string tempX = inputArrayX[i].ToString();
            Keys keys = mapToKeyboardMacro(tempX);
            KeyboardSimulator.KeyDown(keys);
        }
        KeyboardSimulator.KeyPress(Keys.Enter);
        MouseSimulator.Click(MouseButton.Left);

        //reset mouse to user pos.
        Cursor.Position = new Point(x, y);

        needUnblock = true;

        //inputBlocker();
    }

    private Keys mapToKeyboardMacro(string key)
    {
        if (key == "space")
        {
            return Keys.Space;
        }
        else if (key == "a")
        {
            return Keys.A;
        }
        else if (key == "b")
        {
            return Keys.B;
        }
        else if (key == "c")
        {
            return Keys.C;
        }
        else if (key == "d")
        {
            return Keys.D;
        }
    }

【问题讨论】:

    标签: c# macros key


    【解决方案1】:

    您永远不会从 KeyboardSimulator 触发 KeyUp 命令。当键按下时,它不能再次按下。您必须让 KeyUp 才能触发新的 KeyDown 事件。

    【讨论】:

    • 杰夫你是第一个得到我的 +1。不知道为什么我的回答被接受了,但我觉得这个答案和我的一样好。
    • 谢谢 - 但我确实认为您的回答更清楚一点,因为他只有一个电话可以与您联系,两个电话可以与我的解决方案联系。无论哪种方式,猫都会被剥皮,一天就结束了。所以我马上就给你+1。
    【解决方案2】:

    试试changing KeyboardSimulator.KeyDown(keys); 使用KeyboardSimulator.KeyPress(keys);

    我不确定 KeyDown 事件是否会检查键的状态(如果它已经关闭)..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-19
      • 2012-07-12
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多