开发者

problem with keyboard simulate press event

开发者 https://www.devze.com 2022-12-12 12:28 出处:网络
im trying to make a automated tool for poulating textboxes with usernames for a dispatcher application at work.

im trying to make a automated tool for poulating textboxes with usernames for a dispatcher application at work.

Im having some problems trying to simultate key press, if the inputArrayX[i] array contains a,b,c

the keyboardsim will press abc, but if the Array contains a,b,b,c,c it still only types out abc, and not abbcc like i want it to do.

anyone have a clue what im doing wrong here?

    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;
        }
    }


You are never firing the KeyUp command from your KeyboardSimulator. When the key is down it cannot be pressed again. You have to let the KeyUp in order to fire a new KeyDown event.


Try changing KeyboardSimulator.KeyDown(keys); to use KeyboardSimulator.KeyPress(keys);

I'm not sure if the KeyDown Events will check the state of the key if it's already down..

0

精彩评论

暂无评论...
验证码 换一张
取 消