Converting Strings to keyCodes..

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

import java.io.*;

import java.awt.AWTException;

publicclass AutoTyperimplements ActionListener{

publicstatic JTextArea typingArea =new JTextArea();

publicstaticvoid main(String args[])throws AWTException{

new AutoTyper();

}

/* Creates a default constructor */

public AutoTyper()throws AWTException{

JFrame mainFrame =new JFrame("AutoTyper ");

mainFrame.pack();

mainFrame.setResizable(false);

mainFrame.setSize(new Dimension(300, 300));

mainFrame.setLayout(null);

typingArea.setBounds(100, 100, 80, 40);

mainFrame.getContentPane().add(typingArea);

JButton start =new JButton("Start!");

start.addActionListener(this);

start.setBounds(150, 200, 80, 40);//X Y WIDTH HEIGHT

mainFrame.getContentPane().add(start);

JButton stop =new JButton("Stop!");

stop.addActionListener(this);

stop.setBounds(50, 200, 80, 40);

mainFrame.getContentPane().add(stop);

mainFrame.setVisible(true);

}

publicvoid actionPerformed(ActionEvent e){

try{

JButton b = (JButton) e.getSource();

Robot bot =new Robot();

if(b.getText().equals("Start!")){

System.out.println("Starting...");

String sentence = typingArea.getText();

String[] letters =new String[sentence.length()];

for(int i = 0; i < sentence.length(); i++){

char c = sentence.charAt(i);

String lettersTest = letters.toString();

bot.keyPress(c);

bot.keyRelease(c);

System.out.println(lettersTest);

}

}

}catch(AWTException ex){

ex.printStackTrace();

}

}

}

What i've got so far, but im confused...How do I convert strings to keycodes? I've tried many different ways but to no avail.

[3733 byte] By [Shadow1222a] at [2007-11-27 8:43:48]
# 1

Converting characters to virtual key codes is always going to be a problem, because some characters don't have any key associated with them, for eg: % is actually two keys, and ?can be typed as alt-0198 (on Windows). So obviously, you can't just turn any character into a keycode.

That said, if you restrict yourself to letters of the alphabet you could try something like:

int offset = myCharacter - 'a';

int code = KeyEvent.VK_A + offset;

That relies on the convenient fact that each of the alphabetic key codes follow eachother one by one. It's a bit of a hack really, and not considered good form, but it should get the job done.

The other thing you can do, for the special characters, is a big switch statement, eg:

switch(myChar) {

case '&': return VK_AMPERSAND;

break;

case ...

}

That sort of thing is slow, relatively speaking, and means a lot of tedious coding, so restrict it to what you really need.

Good luck.

Widjeta at 2007-7-12 20:44:20 > top of Java-index,Desktop,Core GUI APIs...
# 2
Save the KeyEvents and playback the events. Here is a simple example: http://forum.java.sun.com/thread.jspa?forumID=256&threadID=406735
camickra at 2007-7-12 20:44:20 > top of Java-index,Desktop,Core GUI APIs...
# 3

This is the least I can do for you =) I hope it helps for you. I made the program print each character at a time.

I used JPasswordField instead of JTextArea so the characters typed in are private this time =) So when you type in the code and hit start, the program will print everything character by character.... Good Luck

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

import java.io.*;

import java.awt.AWTException;

public class AutoTyper implements ActionListener {

public static JPasswordField typingArea = new JPasswordField();

public static void main(String args[]) throws AWTException {

new AutoTyper();

}

/* Creates a default constructor */

public AutoTyper() throws AWTException {

JFrame mainFrame = new JFrame("AutoTyper ");

mainFrame.pack();

mainFrame.setResizable(false);

mainFrame.setSize(new Dimension(300, 300));

mainFrame.setLayout(null);

typingArea.setBounds(100, 100, 80, 40);

mainFrame.getContentPane().add(typingArea);

JButton start = new JButton("Start!");

start.addActionListener(this);

start.setBounds(150, 200, 80, 40);//X Y WIDTH HEIGHT

mainFrame.getContentPane().add(start);

JButton stop = new JButton("Stop!");

stop.addActionListener(this);

stop.setBounds(50, 200, 80, 40);

mainFrame.getContentPane().add(stop);

mainFrame.setVisible(true);

}

public void actionPerformed(ActionEvent e) {

try {

JButton b = (JButton) e.getSource();

Robot bot = new Robot();

char[] pWord = typingArea.getPassword();

if(b.getText().equals("Start!")) {

System.out.println("Starting...");

for(int i = 0; i < pWord.length; i++) {

//bot.keyPress(c);

//bot.keyRelease(c);

System.out.print(pWord[i]);

}

System.out.println();

}

} catch(AWTException ex) {

ex.printStackTrace();

}

}

}

CrazyJavaa at 2007-7-12 20:44:20 > top of Java-index,Desktop,Core GUI APIs...
# 4
I don't think that's what he wants, CrazyJava - the problem is that Robot takes key codes rather than characters, not that he can't get the characters from the field.Actually, I'd advocate camickr's approach - more elegant by far, though slightly trickier.
Widjeta at 2007-7-12 20:44:20 > top of Java-index,Desktop,Core GUI APIs...
# 5
Yeah I see... I took his code and revised it quickly, copy pasted it back to the forum, heh, my apologies.. =D
CrazyJavaa at 2007-7-12 20:44:20 > top of Java-index,Desktop,Core GUI APIs...