tags:

views:

79

answers:

2

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;
        }
    }
+2  A: 

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.

Jeff
ah ofc, thanks mate for pointing that out.
Darkmage
Jeff you got a +1 from me for being the first.. Not sure why my answer got accepted, but i felt this was as good an answer as mine.
Michael G
Thanks - but I do think your answer was a bit clearer being the fact he only has one call to make with your and two to make with my solution. Either way the cat gets skinned and the day is done. So +1 from me right back at you.
Jeff
+2  A: 

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..

Michael G
very good, just what i was after. how did i not se that.
Darkmage