How to send keys by..

How do you send keys(as in type) using java? Without doing it manually that is.
[86 byte] By [Shadow1222a] at [2007-11-27 8:38:34]
# 1
Use a Robot.Dispatch a KeyEvent.
camickra at 2007-7-12 20:36:18 > top of Java-index,Desktop,Core GUI APIs...
# 2
Can you give me an example?
Shadow1222a at 2007-7-12 20:36:18 > top of Java-index,Desktop,Core GUI APIs...
# 3
No. Your question is too vague, so I don't really know what you are trying to do. You can search the forum. for examples of using the Robot class and the dispatchEvent(...) method.
camickra at 2007-7-12 20:36:18 > top of Java-index,Desktop,Core GUI APIs...
# 4
Sorta like an auto typer, it takes the text from a JTextArea and types them so the user doesn't have to multiple times.
Shadow1222a at 2007-7-12 20:36:18 > top of Java-index,Desktop,Core GUI APIs...
# 5

You still haven't defined the requirements. Why do you do this? When do you do this?

a) When the text area is full

b) when the use clicks a button

c) for every character that is typed

Ask a clear question with clear requirements and you might get an answer...tomorrow... because I'm signing off soon.

camickra at 2007-7-12 20:36:18 > top of Java-index,Desktop,Core GUI APIs...
# 6
Okay, when a start button is clicked it gets the text from the JTextArea and types it multiple times at a certain rate. Get it now?(It types every character inside the JTextArea
Shadow1222a at 2007-7-12 20:36:18 > top of Java-index,Desktop,Core GUI APIs...
# 7
textComponent.getDocument().insertString(....);You can use a Timer to schedule the interval of the insertion. "How to Use Timers": http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html
camickra at 2007-7-12 20:36:18 > top of Java-index,Desktop,Core GUI APIs...
# 8
Thats not what I want, sorry I didn't word it correctly, I want everything I stated above, but I need it to type into other windows like, if im playing a game it will auto-type what I put into the JTextArea
Shadow1222a at 2007-7-12 20:36:18 > top of Java-index,Desktop,Core GUI APIs...
# 9

Well the Robots class is what you want then. Use a timer to trigger the Robot keyPress() and keyRelease() methods:

eg:

Robot bot = new Robot(); //throws an exception, so you need a try block

...in timer event handling code...

for (char c : stringOfCharsToType) {

int keyCode = convertCharToKeyCode(c);

bot.keyPress(keyCode);

bot.keyRelease(keyCode);

}

The hardest bit is then to convert the characters to virtual key codes (eg: KeyEvent.VK_A etc).

Hope that helps.

Widjeta at 2007-7-12 20:36:18 > top of Java-index,Desktop,Core GUI APIs...