Results 1 to 19 of 19
- 09-12-2011, 03:02 AM #1
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
There is my SSCCE :)Java 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); } }
My question is why when it fires the event, the character 'A' does not show up in the text box?Last edited by crikey; 09-12-2011 at 03:54 AM.
- 09-12-2011, 03:09 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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:
Java Code:tf1.requestFocusInWindow(); //tf1.setCaretPosition(...); tf1.replaceSelection("a");
- 09-12-2011, 03:17 AM #3
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
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:
I get an invalid key code errorJava Code:tf1.cutButton.processEvent(new KeyEvent(tf1.cutButton, KeyEvent.KEY_TYPED, System.currentTimeMillis(), 0, KeyEvent.VK_A, 'a'));
- 09-12-2011, 03:26 AM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.Last edited by camickr; 09-12-2011 at 03:29 AM.
- 09-12-2011, 03:30 AM #5
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
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
- 09-12-2011, 03:54 AM #6
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
Aha Thanks! It worked :)
- 09-25-2011, 07:14 PM #7
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
OH one problem now. VK_ENTER does not work. This is how i send an 'a':
That works. But when i try enter:Java Code:MainFrame.getApplet() .getComponentAt(1, 1) .dispatchEvent( new KeyEvent(MainFrame.getApplet() .getComponentAt(1, 1), KeyEvent.KEY_TYPED, System.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, 'a'));
It doesen't work :(. How should i simulate enter?Java 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));
- 09-25-2011, 09:35 PM #8
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
I think you need to use the keyPressed/keyReleased events.
- 10-01-2011, 02:42 AM #9
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
it still doesent work :(Java Code:MainFrame.getApplet() .getComponentAt(1, 1) .dispatchEvent( new KeyEvent(MainFrame.getApplet() .getComponentAt(1, 1), KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, 'a'));
- 10-03-2011, 12:07 AM #10
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
Anybody? . . . .
- 10-03-2011, 01:33 AM #11
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 10-03-2011, 04:29 AM #12
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
Is KeyBinding not applicable in this situation?
- 10-03-2011, 06:11 AM #13
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
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
- 10-03-2011, 07:42 AM #14
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 10-04-2011, 01:12 AM #15
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
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"
Here is my SSCCE:Java 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]
How do i build the enter with that information?Java 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); } }
- 10-04-2011, 06:19 AM #16
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
What are 64, 16, '\0 supposed to represent??? Don't use magic numbers!!!
Java Code:tf.dispatchEvent( new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_ENTER, (char)KeyEvent.VK_ENTER) );
- 10-04-2011, 06:55 AM #17
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)
not sure, it was there on accident :)
And that doesent work up there ^
- 10-04-2011, 07:09 AM #18
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 10-04-2011, 09:56 AM #19
Similar Threads
-
Polling mouse status directly without MouseEvent?
By Toll in forum AWT / SwingReplies: 4Last Post: 06-05-2011, 02:16 PM -
Exception occurred during event dispatching: java.lang.NullPointerException
By oliveira in forum JDBCReplies: 17Last Post: 04-13-2011, 06:18 PM -
MouseEvent
By PhQ in forum Java AppletsReplies: 21Last Post: 03-26-2010, 08:48 PM -
SWT analog of Swing FocusEvent.getOppositeComponent?
By asdqwezx in forum SWT / JFaceReplies: 0Last Post: 07-16-2009, 12:52 AM -
JBoss and Java 6:Exception occurred during event dispatching
By Ed in forum Advanced JavaReplies: 2Last Post: 07-04-2007, 05:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks