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

    Smile How to dispatchEvent(KeyEvent evt) to an applet

    First off, I apologize for not having an SSCCE code snippet. In the case that we do need one, I will create one :)

    My question & scenario: I have an Applet that I want to send mouse and key strokes into. I studied that dispatchEvent could do the job, but it requires a KeyEvent object. I'm not sure what to place where. In the end, it can send mouse and keystrokes even when the applet's window is minimized. I also want it at the point where I can send the letter 'a' like this:
    sendKey('a');
    Do i need to add a actionlistener or keylistener to an applet? If so, how? Thank you very much :)
    PS: I looked all over the forums, but could not find anything on this very subject.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    I want to send mouse and key strokes into
    Are you saying you want to create new Event objects of those types and pass them to the components in the Applet via its event queue without actually involving moving the cursor or pressing on the keyboard?

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

    Default

    thanks for your reply Norm,
    Yes, I that is precisely what I want

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    You didn't ask any further questions, so I'll assume you know how to do it

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

    Default

    I dont know how to do it. I guess based off what you said two posts earlier, I will change my question: how can i send text and mouse movements to an applet?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Those are sort of vague requirements. Can you be more specific?
    Is there a component in the applet that you want to receive text? Like a text field?
    And what about the mouse movements? What component is to receive them and what will it do with them?

    What class is sending these events and how is it related to the applet?

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

    Default

    The applet is a game, so there aren't other components in it. As for the mouse, I wanted to send mouse movements/strokes (not using the main mouse) to this Applet named "applet".
    I heard you could use applet.dispatchEvent(KeyEvent evt), but I'm not sure where to go from there.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    there aren't other components
    Interesting. All GUI is built from java components.

    What class/program do you want to send these events and how is it related to the applet?

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

    Default

    Suppose I have a class named "MouseHandler". Inside is a method named "sendStringToApplet(String text)". Inside this method, it dispatches an event to the static Applet "applet" in the AppletGUI class. For example, The applet is already open, so i run the method "sendStringToApplet("hello world!");". It should be able to send text to the applet. Where the text goes in the Applet does not matter for this conversation, Trust me :). So how can i get this method to work?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Where the text goes in the Applet does not matter for this conversation
    That makes no sense at all. I have written dozens of applets that you could "send" a message to and the applet would completely ignore it. They have no listeners for any text messages.
    So why send a message if there is nothing/no one to receive it?

    Please explain.

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

    Default

    The applet is a game that uses its gaming engine to draw out objects. java.awt.Robot worked with sending keys and moving the mouse, which proves it works. But I can't use robot in my case because It will disrupt normal computing.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    You still have not explained the relationship between the code you want to write and the applet code. Does your code load the applet's classes and call its methods in the same JVM?
    Does the applet run from an HTML page in a browser and your code is a separate desktop app?
    Please explain.

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

    Default

    [quote = Does your code load the applet's classes and call its methods in the same JVM? [/quote]

    Yes, I load it from an external URL, and it displays on my desktop application. This is all from the viewpoint of my Desktop application.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    By loading the Applet I meant, does your code create an instance of the applet using the new statement. Something like the following:
    Applet theApplet = new THEAPPLET();
    Then your code calls the applet's methods:
    theApplet.init();
    theApplet.start();
    etc

    I load it from an external URL, and it displays on my desktop application.
    Do you have a classloader that reads the jar file with the class files from the URL and loads the classes from that jar file or what does your code do to "load" the applet?
    Your desktop application provides the window/frame that the applet displays in.

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

    Default

    Quote Originally Posted by Norm View Post
    By loading the Applet I meant, does your code create an instance of the applet using the new statement. Something like the following:
    Applet theApplet = new THEAPPLET();
    Then your code calls the applet's methods:
    theApplet.init();
    theApplet.start();
    etc


    Do you have a classloader that reads the jar file with the class files from the URL and loads the classes from that jar file or what does your code do to "load" the applet?
    Your desktop application provides the window/frame that the applet displays in.
    Yes, i use the new statement. And i do run applet.init() to start it. And what you said is how i load the applet (using the classloader) onto my application.

    "Your desktop application provides the window/frame that the applet displays in."
    yes

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Can you get references to the components you want to send events to?
    Here is a technique I used to send an ActionEvent to a component to simulate pressing Enter:
    Java Code:
             Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(
                             new ActionEvent(src,   // the component to get the event
                             ActionEvent.ACTION_PERFORMED, 
                             "Enter"));

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

    Default

    Thanks Norm. I set the applet to be static so I wouldn't have to instantiate it. So now, for the "src", i use AppletUI.applet. Now, how about mouse movements? And can I replace "Enter" with "A" if i want it to send a? Or how does it work? Do i need to attack a keylister to the applet and dispatch the event?

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    how about mouse movements?
    There are event classes for mouse movements.
    can I replace "Enter" with "A" if i want it to send a
    Don't know, I've only used what I posted. To send a key event you would need to use the class for that.

Similar Threads

  1. Help in KeyEvent
    By chyrl in forum AWT / Swing
    Replies: 15
    Last Post: 05-26-2010, 08:12 AM
  2. JOptionPane KeyEvent
    By mine0926 in forum NetBeans
    Replies: 5
    Last Post: 05-05-2010, 02:03 AM
  3. Replies: 11
    Last Post: 12-25-2009, 10:30 PM
  4. Replies: 0
    Last Post: 08-17-2009, 03:59 PM
  5. Replies: 4
    Last Post: 06-08-2007, 06:15 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
  •