HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Simple easy-to-use AutoTyper

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
easysimpleautotyperuse

Problem

I have created an auto-typer that runs through a simple little GUI.This was my first ever Java program so it's pretty simple and not anything special, but I'm curious to know if there's anything that I could have done better.

```
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class AutoTyper{

private static int interval;
private static Timer timerMain;
private static JTextField txtSpam;
private static JTextField txtInterval;
private static JButton btnStart;
private static JButton btnStop;

public static void main(String[]args){
ActionListener taskSpam = new ActionListener(){
public void actionPerformed(ActionEvent evt) {
sendkeys(txtSpam.getText());
}
};
ActionListener taskStartTimer = new ActionListener(){
public void actionPerformed(ActionEvent evt){
timerMain.setDelay(Integer.parseInt(txtInterval.getText()));
timerMain.start();
btnStart.setEnabled(false);
btnStop.setEnabled(true);
txtInterval.setEnabled(false);
}
};
ActionListener taskStopTimer = new ActionListener(){
public void actionPerformed(ActionEvent evt){
timerMain.stop();
btnStart.setEnabled(true);
btnStop.setEnabled(false);
txtInterval.setEnabled(true);
}
};

btnStart = new JButton("Start Spam");
btnStop = new JButton("Stop Spam");
btnStart.setBackground(Color.BLACK);
btnStart.setForeground(Color.GREEN);
btnStop.setBackground(Color.BLACK);
btnStop.setForeground(Color.GREEN);
JLabel lbl1 = new JLabel("Text to Spam");
JLabel lbl2 = new JLabel("Interval:");
timerMain = new Timer(1,taskSpam);
txtSpam = new JTextField("", 13);
txtSpam.setBackground(Color.BLACK);
txtSpam.setForegrou

Solution

Not used variables

I don't think this variable is used:

private static int interval;


Method

If you want to type the text without having to do each letter at a time, I'd use a Map to keep track of what code relates to each letter. Something like:

// earlier on in your class, set up the map
   private static Map charMap;

   static {
      charMap = new HashMap();
      charMap.put( ' ', 32 );
      charMap.put( 'a', 65 );
      charMap.put( 'b', 66 );
      charMap.put( 'c', 67 );
      charMap.put( 'd', 68 );
      charMap.put( 'e', 69 );
      charMap.put( 'f', 70 );

      // ... and so on until ...
      charMap.put( '0', 48 );
   }

   // now define the method to use it
   public static void sendText(String aa) {

      for ( int i = 0; i < aa.length(); i++ ) {
         robot.keyPress( charMap.get( aa.charAt( i ) ) );
      }
   }


Exception Handling

In the case of creating a Robot, if you get an AWTException as you have, remember that this means no Robot was created so you can't rely on it later.
Remember also to show the exception description. Typing "error" or "unknown error" isn't so fair for me, you can easily get the description of the exception.

Code Snippets

private static int interval;
// earlier on in your class, set up the map
   private static Map<Character,Integer> charMap;

   static {
      charMap = new HashMap<Character,Integer>();
      charMap.put( ' ', 32 );
      charMap.put( 'a', 65 );
      charMap.put( 'b', 66 );
      charMap.put( 'c', 67 );
      charMap.put( 'd', 68 );
      charMap.put( 'e', 69 );
      charMap.put( 'f', 70 );

      // ... and so on until ...
      charMap.put( '0', 48 );
   }

   // now define the method to use it
   public static void sendText(String aa) {

      for ( int i = 0; i < aa.length(); i++ ) {
         robot.keyPress( charMap.get( aa.charAt( i ) ) );
      }
   }

Context

StackExchange Code Review Q#110704, answer score: 6

Revisions (0)

No revisions yet.