Aldo Aceves
Aldo Aceves

Reputation: 1

When I try to use an awt robot to type in java nothing comes out, what am I doing wrong?

When I try to use an awt robot to type in java nothing comes out,

what am I doing wrong?

I wanted to make something that would just type TEST and a new line after but every time I run it nothing comes out, I wonder what it is that I'm doing wrong. Pardon my nooby code I am new to Java.

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

public class test {

    static int msg[] = 
    {
        KeyEvent.VK_T, KeyEvent.VK_E, KeyEvent.VK_S, KeyEvent.VK_T, KeyEvent.VK_ENTER,
    };

    public static void main(String[] args) throws AWTException, InterruptedException {
        Robot r = new Robot();

        Thread.sleep(200);
        for(int x = 0;x < 10;x++){
            for(int i = 0;i < msg.length;i++){
                r.keyPress(msg[i]);
                r.delay(10);
            }
        }

    }

}

Upvotes: 0

Views: 82

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347204

  1. Make sure you follow the keyPress with a keyRelease call (for the same key type)
  2. Make sure that the wherever you want the text to come out has key board focus

Upvotes: 1

ACV
ACV

Reputation: 10562

Where is it typing? If it is AWT, you need to get control focus first.

Upvotes: 0

Related Questions