• Log in or sign up for an account here: https://www.sythe.org/login

Crappy auto talker

Fate1

Apprentice
Banned
Joined
Apr 21, 2005
Sythe Gold
5
Messages
773
Age
34
Crappy auto talker
Can't get it to start or stop with the f keys but other than that i think it's decent. Tried using hashmap with it and I don't know why i did it and noticed this was an easier way.
EDIT: i just took out the keylistener.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * Fate's AutoTalker.
 *
 * @author Fate
 * @version v1.0 1/2/10
 */
public class GUIAutoTalkerFrame extends JFrame implements ActionListener {
	JButton start = new JButton("Start");
	JButton stop = new JButton("Stop");
	JButton quit = new JButton("Quit");
	JTextField message = new JTextField("Enter your message here.");
	JLabel delayLbl = new JLabel("Delay: ");
	JTextField delay = new JTextField("1000", 3);
	JPanel p1 = new JPanel();
	Timer timer;

    public GUIAutoTalkerFrame() {
     	setTitle("AutoTalker: f3=Stop, f4=Start");
     	setResizable(false);
     	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     	timer = new Timer(1, autoTalk);
     	stop.setEnabled(false);
     	
     	start.addActionListener(startTask);
     	stop.addActionListener(stopTask);
     	quit.addActionListener(this);
		
     	p1.add(message);
     	p1.add(delayLbl);
     	p1.add(delay);
     	p1.add(start);
     	p1.add(stop);
     	p1.add(quit);
     	
     	add(p1);
     	
     	pack();
     	setVisible(true);
                
	}

	public void actionPerformed(ActionEvent evt) {
		Object source = evt.getSource();
		
		if (source.equals(quit)) {
			quit();
		}
	}
	
	ActionListener startTask = new ActionListener() {
		public void actionPerformed(ActionEvent evt) {
			timer.setDelay(Integer.parseInt(delay.getText()));
			timer.start();
			start.setEnabled(false);
			stop.setEnabled(true);
			quit.setEnabled(true);
			delay.setEnabled(false);
			message.setEnabled(false);
		}
	};
	ActionListener stopTask = new ActionListener() {
		public void actionPerformed(ActionEvent evt) {
			timer.stop();
			start.setEnabled(true);
			stop.setEnabled(false);
			quit.setEnabled(true);
			delay.setEnabled(true);
			message.setEnabled(true);
		}
	};
	ActionListener autoTalk = new ActionListener() {
		public void actionPerformed(ActionEvent evt) {
			talk(message.getText());
		}
	};
	
	public void talk(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 (AWTException ae) {
			System.out.println("Error: " + ae.getMessage());
		}
	}
	public void quit() {
		System.exit(0);
	}
	public static void main(String[] args) {
		GUIAutoTalkerFrame frame = new GUIAutoTalkerFrame();
	}
	
}
 
Crappy auto talker
The reason your hotkeys don't work is because you don't understand how KeyListeners work. You cannot hook the Keyboard so that your method is called whenever a Key is pressed, as you can in lower-level languages. Instead, the keyPressed() method is called to whatever component the KeyListener is added to via the addKeyListener() method (very similarly to how ActionListeners and FocusListeners work). This means that you cannot use hotkeys as you intend to, and so you would remove the code pertaining to that.

As another word of advice, you shouldn't compare Objects via ==, but rather through the equals() method (look at your actionPreformed() method to see what you should fix).

Your key-pressing code also will not work, even though it compiles okay. It won't work, because it takes the KeyCode int as a param, rather then the actual char that you want pressed. For this, you should use the HashMap that you made in your earlier thread.

You also should place your talk() and quit() voids after the default constructor, and you shouldn't have your ActionListener fields declared in the middle of where your methods are located.

With the above few problems fixed, your code should run fine.
 
Crappy auto talker
Okay i'll try to update it a little with your advice and let you know how it goes. Thanks for the comments
EDIT: updated the code. I just erased the key listener. Still couldn't find out how to get it to stop using the f3 key while it was running.
 
Runescape Gamblingrunescape gambling
Back
Top