Results 1 to 19 of 19
  1. #1
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)

    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);
    	}
    }
    There is my SSCCE :)
    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.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default 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");

  3. #3
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default 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:
    Java 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

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default 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.

  5. #5
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default 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

  6. #6
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)

    Aha Thanks! It worked :)

  7. #7
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)

    OH one problem now. VK_ENTER does not work. This is how i send an 'a':
    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'));
    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, (char) KeyEvent.VK_ENTER));
    It doesen't work :(. How should i simulate enter?

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)

    I think you need to use the keyPressed/keyReleased events.

  9. #9
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)

    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'));
    it still doesent work :(

  10. #10
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)

    Anybody? . . . .

  11. #11
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default 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.

  12. #12
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)

    Is KeyBinding not applicable in this situation?

  13. #13
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default 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

  14. #14
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default 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.

  15. #15
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default 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"

    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]
    Here is my SSCCE:

    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);
        }
    }
    How do i build the enter with that information?

  16. #16
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

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

  17. #17
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)

    not sure, it was there on accident :)

    And that doesent work up there ^

  18. #18
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default 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.

  19. #19
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Dispatching event to Component (KeyEvent, FocusEvent, MouseEvent)

    Quote Originally Posted by crikey View Post
    Well honestly, I am making a game bot <snip/>
    Anyone else see the irony in that statement?

    db

Similar Threads

  1. Replies: 4
    Last Post: 06-05-2011, 02:16 PM
  2. Replies: 17
    Last Post: 04-13-2011, 06:18 PM
  3. MouseEvent
    By PhQ in forum Java Applets
    Replies: 21
    Last Post: 03-26-2010, 08:48 PM
  4. SWT analog of Swing FocusEvent.getOppositeComponent?
    By asdqwezx in forum SWT / JFace
    Replies: 0
    Last Post: 07-16-2009, 12:52 AM
  5. Replies: 2
    Last Post: 07-04-2007, 05:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •