4
\$\begingroup\$

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.setForeground(Color.GREEN);
        txtInterval = new JTextField("3000",3);
        txtInterval.setBackground(Color.BLACK);
        txtInterval.setForeground(Color.GREEN);

        btnStart.addActionListener(taskStartTimer);
        btnStop.addActionListener(taskStopTimer);
        btnStop.setEnabled(false);

        JPanel intervalpane = new JPanel();
        intervalpane.add(lbl2,BorderLayout.EAST);
        intervalpane.add(txtInterval,BorderLayout.WEST);

        JPanel bottompane = new JPanel();
        bottompane.add(btnStart,BorderLayout.EAST);
        bottompane.add(btnStop,BorderLayout.CENTER);
        bottompane.add(intervalpane,BorderLayout.WEST);

        JPanel toppane = new JPanel();
        toppane.add(lbl1,BorderLayout.EAST);
        toppane.add(txtSpam,BorderLayout.NORTH);

        JPanel pane = new JPanel(new BorderLayout());
        pane.add(toppane,BorderLayout.NORTH);
        pane.add(bottompane,BorderLayout.SOUTH);

        JFrame frame = new JFrame("Auto Typer");
        frame.setForeground(Color.BLACK);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(pane);
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);
      }

    private static void sendkeys(String text) {
        try {
            Robot robot = new Robot();
            text = text.toUpperCase();
            for(int i = 0; i < text.length(); i++)
                robot.keyPress(text.charAt(i));
        robot.keyPress(KeyEvent.VK_ENTER);
        }catch(java.awt.AWTException exc) {
            System.out.println("error");
        }
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ In addition to what @DrProgrammer said: - Use JavaFX. Why? Swing is deprecated and therefore, your program is not future proof as it will be removed sometime in the future. Moreover, JavaFX is much more easy and simpler to use. \$\endgroup\$
    – codez
    Commented Nov 14, 2015 at 7:06

1 Answer 1

6
\$\begingroup\$

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<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 ) ) );
      }
   }

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.

\$\endgroup\$
3
  • \$\begingroup\$ The private static int variable is used for the interval time. This is probably a noob question but in the map, what is the number for? charMap.put( 'f' , 70) <- what's the number mean? \$\endgroup\$
    – 13aal
    Commented Nov 14, 2015 at 2:52
  • 1
    \$\begingroup\$ @LostBam I think you confound what I said. There isn't any private static int variable, but private static int interval;. No, it isn't used. Try to delete that, you'll see that your program works ... An HashMap is a special kind of associative array (called also dictionary or map) which associates generic keys to generic values. The HashMap is not the only one existing, a TreeMap, for example, exists too, which provides roughly the same interface but a totally different implementation. That number, as a parameter, is the value to be associated with the specified key. \$\endgroup\$ Commented Nov 14, 2015 at 12:00
  • \$\begingroup\$ oh so it's basically just a hash or dict. That's awesome! Thanks guys \$\endgroup\$
    – 13aal
    Commented Nov 14, 2015 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.