java.awt.Robot.keyPress for continuous keystrokes

Posted by Deb on Stack Overflow See other posts from Stack Overflow or by Deb
Published on 2012-04-09T05:08:20Z Indexed on 2012/04/09 5:29 UTC
Read the original article Hit count: 228

Filed under:
|
|
|
|

So, here's my problem. I have a java program which will send keystroke messages to a game (built in Unity), based on how the user interacts with an android phone. (My java program is a listener for the android interaction over wi-fi) Now, in order to do this, I am using java.awt.Robot to send keyPresses to the game window. I have the following code block written in my listener program:

if(interacting)
{
    Robot robot = new Robot();
    robot.keyPress(VK_A);
    robot.delay(20); //to simulate the normal keyboard rate
}

Now the variable interacting will be true as long as the user presses down on the touch screen of the phone, and what I intend to achieve is a continuous chain of keystroke messages being delivered to the game (through the listener). However, this is severely affecting performance, for some reason. I am noticing that the game becomes slow (rapidly dropping frame rates), and even the computer becomes slow, in general. What's going wrong? Should I use a robot.keyRelease(VK_A) after each keyPress? But my game has a different action mapped to the release of a key, and I do not want rapid key presses and releases; what I really want is to simulate continuous keystrokes, in exactly the way it would behave if the user were pressing down the A key on their keyboard manually. Please help.

© Stack Overflow or respective owner

Related posts about java

Related posts about eclipse