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

    Default Simulating a key press (not click) in a component

    I'm trying to create a method where you send a KeyEvent to a component, specifically a key press.

    For example:

    public void pressKey(int KeyCode, int ms){
    //...
    }

    The method would hold a key down, like KeyEvent.UP for ms milliseconds. How should I do this? What will the constructed KeyEvent look like? Thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Simulating a key press (not click) in a component

    Robot can send key presses and releases, and I suggest you have a look at the Robot API for more on this. If you're still stuck after reviewing this, then consider providing more details on your problem -- what component, what type of program, etc...

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

    Default Re: Simulating a key press (not click) in a component

    Oh, robot won't work for my specific application because the events need to be sent even while the window is minimized. This component is an applet.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default Re: Simulating a key press (not click) in a component

    What will the constructed KeyEvent look like?
    Have you looked at the API doc for KeyEvent to see how to code its constructor?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Simulating a key press (not click) in a component

    Quote Originally Posted by crikey View Post
    Oh, robot won't work for my specific application because the events need to be sent even while the window is minimized. This component is an applet.
    Again, please clarify as much as possible as you're leaving way too much unsaid. So am I correct in assuming that the user is running an applet (JApplet?) that is minimized, and that you want to send programmatic key presses to this applet and not text? What component on the applet is supposed to accept the key presses? This is a *very* unusual requirement, one that needs some explanation as to its purpose. Or is it that the applet minimized and you're trying to send key presses to another unrelated application, one that has the focus?

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

    Default Re: Simulating a key press (not click) in a component

    Well, this is hacking program for java applet based games. The component receiving the events is simply the applet.getComponentAt(1,1), Which is a canvas I believe. I can already make it send key clicks for text, but I'm wondering how to make it send key presses for Something like KeyEvent.UP/DOWN/RIGHT/LEFT

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default Re: Simulating a key press (not click) in a component

    Did you look at the API doc for the KeyEvent class's constructor?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Simulating a key press (not click) in a component

    Yes, but looking at the API doc never helps me

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Simulating a key press (not click) in a component

    Quote Originally Posted by crikey View Post
    Yes, but looking at the API doc never helps me
    Then you need to do it more often. Just like any other skill, it gets better with practice. For instance, it would have told you that your Integer.parseInteger(...) method doesn't even exist, and that the method that does exist, parseInt(...) would not have worked as you suggested it should.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Simulating a key press (not click) in a component

    Regarding your original question, if you're still stuck, then please do read the API as Norm suggests, and if you're confused or hung up on something, ask us to help explain it to you. Also, please consider answering the other issues I have asked in my posts above.

Similar Threads

  1. Simulating a car race
    By Chygoz in forum New To Java
    Replies: 5
    Last Post: 02-18-2012, 08:43 PM
  2. Replies: 2
    Last Post: 06-22-2011, 01:19 PM
  3. Replies: 0
    Last Post: 09-04-2009, 07:54 AM
  4. simulating netstat
    By prashant in forum Networking
    Replies: 1
    Last Post: 03-14-2009, 07:41 AM
  5. Press any key to continue/press enter
    By JT4NK3D in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 09:27 PM

Posting Permissions

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