-
Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
Code:
import java.awt.AWTEvent;
import java.awt.Frame;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JTextField;
public class Main extends Frame {
MyButton cutButton;
public Main() {
setSize(450, 250);
cutButton = new MyButton("");
cutButton.setBounds(10, 10, 100, 100);
cutButton.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent arg0) {
System.out.println(arg0.toString());
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
System.out.println(arg0.toString());
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
cutButton.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
System.out.println(e.toString());
// cutButton.dispatchEvent(e);
}
});
cutButton.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent arg0) {
System.out.println("Focus gained!");
}
@Override
public void focusLost(FocusEvent arg0) {
System.out.println("focus lost!");
}
});
add(cutButton);
}
public static void main(String args[]) throws InterruptedException {
Main tf1 = new Main();
tf1.setVisible(true);
for (int i = 0; i < 10; i++) {
Thread.sleep(1000);
tf1.cutButton.processEvent(new FocusEvent(tf1.cutButton,
FocusEvent.FOCUS_FIRST));
tf1.cutButton.processEvent(new MouseEvent(tf1.cutButton,
MouseEvent.MOUSE_CLICKED, System.currentTimeMillis(), 0,
20, 20, 20, 20, 1, false, MouseEvent.BUTTON1));
tf1.cutButton.processEvent(new KeyEvent(tf1.cutButton,
KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0,
KeyEvent.VK_A, (char) KeyEvent.VK_A));
}
}
}
class MyButton extends JTextField {
public MyButton(String label) {
super(label);
}
@Override
public void processEvent(AWTEvent e) {
super.processEvent(e);
}
}
There is my SSCCE :)
My question is why when it fires the event, the character 'A' does not show up in the text box?
-
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
You need to invoke keyTyped() not keyPressed().
Why do you want to do work with Events? You can just use something like:
Code:
tf1.requestFocusInWindow();
//tf1.setCaretPosition(...);
tf1.replaceSelection("a");
-
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
Well honestly, I am making a game bot that uses a virtual mouse and keyboard. I am going to actually use it for a game applet (Runescape).I don't want to use Robot btw. And when i use this:
Code:
tf1.cutButton.processEvent(new KeyEvent(tf1.cutButton,
KeyEvent.KEY_TYPED, System.currentTimeMillis(), 0,
KeyEvent.VK_A, 'a'));
I get an invalid key code error
-
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
I believe the KeyEvent.VK_A is wrong for a keyTyped event. (I think it might be VK_UNDEFINED).
Read the KeyEvent API. It explains what values to use for the keyPressed/keyTyped events.
-
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
KeyEvent (Java 2 Platform SE 5.0)
I read it, but i still can't figure out whats wrong
-
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
-
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
OH one problem now. VK_ENTER does not work. This is how i send an 'a':
Code:
MainFrame.getApplet()
.getComponentAt(1, 1)
.dispatchEvent(
new KeyEvent(MainFrame.getApplet()
.getComponentAt(1, 1), KeyEvent.KEY_TYPED,
System.currentTimeMillis(), 0,
KeyEvent.VK_UNDEFINED, 'a'));
That works. But when i try enter:
Code:
MainFrame.getApplet()
.getComponentAt(1, 1)
.dispatchEvent(
new KeyEvent(MainFrame.getApplet()
.getComponentAt(1, 1), KeyEvent.KEY_TYPED,
System.currentTimeMillis(), 0,
KeyEvent.VK_UNDEFINED, (char) KeyEvent.VK_ENTER));
It doesen't work :(. How should i simulate enter?
-
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
I think you need to use the keyPressed/keyReleased events.
-
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
Code:
MainFrame.getApplet()
.getComponentAt(1, 1)
.dispatchEvent(
new KeyEvent(MainFrame.getApplet()
.getComponentAt(1, 1), KeyEvent.KEY_PRESSED,
System.currentTimeMillis(), 0,
KeyEvent.VK_UNDEFINED, 'a'));
it still doesent work :(
-
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
-
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
The KeyEvent parameters for keyPressed events are different than they are for keyTyped events.
If you need more help then post your SSCCE. All you need is a simple program with a JTextField and then a JButton that will dispatch the event to the text field. Make sure you use all Swing components and don't use any AWT components.
-
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
Is KeyBinding not applicable in this situation?
-
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
Camickr-
The events that I'm using are passed onto an online Applet, and preferably a Canvas in the applet. So an SSCCE would be difficult
-
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
A SSCCE is simple. I suggested how to create one.
The applet has nothing to do with dispatching an event to a component. Your problem is that you don't know how to build the KeyEvent properly, which is why you first test this on a SSCCE. Once you get is working on the SSCCE you apply the knowledge to your real application.
-
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
Okay, i attached a Custom KeyListener, and tried to manually hit enter. Here is the output for "enter"
Code:
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=10,keyText=Enter,keyChar=Enter,keyLocation=KEY_LOCATION_STANDARD,rawCode=13,primaryLevelUnicode=13,scancode=28]
java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar=Enter,keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=0,scancode=0]
java.awt.event.KeyEvent[KEY_RELEASED,keyCode=10,keyText=Enter,keyChar=Enter,keyLocation=KEY_LOCATION_STANDARD,rawCode=13,primaryLevelUnicode=13,scancode=28]
Here is my SSCCE:
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
*
* @author Plasmon
*/
public class JavaApplication2 {
/**
* @param args the command line arguments
*/
public static JTextArea field;
private static class CustomKeyListener implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
System.out.println(e.toString());
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println(e.toString());
}
@Override
public void keyReleased(KeyEvent e) {
System.out.println(e.toString());
}
}
public static void main(String[] args) {
field = new JTextArea();
JFrame frame = new JFrame();
field.addKeyListener(new CustomKeyListener());
frame.setSize(300,300);
frame.setLayout(null);
field.setBounds(0,0,300,300);
JScrollPane pane = new JScrollPane(field);
pane.setBounds(0,0,300,300);
frame.add(pane);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// KeyEvent enterEvent = new KeyEvent(field, KeyEvent.KEY_TYPED, System.currentTimeMillis(),64, 16, '\000', 2);
//field.dispatchEvent(enterEvent);
}
}
How do i build the enter with that information?
-
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
What are 64, 16, '\0 supposed to represent??? Don't use magic numbers!!!
Code:
tf.dispatchEvent( new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
0, KeyEvent.VK_ENTER, (char)KeyEvent.VK_ENTER) );
-
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
not sure, it was there on accident :)
And that doesent work up there ^
-
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
Yes, well that is not how I suggested you create a SSCCE now was it? The code I posted worked for me when I created a proper SSCCE.
-
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
Quote:
Originally Posted by
crikey
Well honestly, I am making a game bot <snip/>
Anyone else see the irony in that statement?
db