Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Is it possible to send keystrokes to a background window?

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Is it possible to send keystrokes to a background window?

    So I have basic java knowledge from 1st year of college and I wrote a program recently that sends keystrokes with the Robot class. Now I would like it to do the same but to send it to a window that isnt my active one.. Ive searched and from what Ive found it seems like you cant do that in java, I need another language like C# (whick sux for me since i dont know any other then java).. Is this true? If not how would i send something to notepad for example while it is not the active window?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,112
    Thanks
    65
    Thanked 2,718 Times in 2,668 Posts

    Default Re: Is it possible to send keystrokes to a background window?

    It should work. Position the location to where the program's window is located and send some key presses.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Is it possible to send keystrokes to a background window?

    Intercepting or "hearing" the keystrokes has to be done by the currently active or focused window, but once received, the keystrokes can be forwarded to any other program object. Write a simple example to accomplish what you've described, and if you have problems with it, post it and ask for help.

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is it possible to send keystrokes to a background window?

    Quote Originally Posted by GregBrannon View Post
    Intercepting or "hearing" the keystrokes has to be done by the currently active or focused window, but once received, the keystrokes can be forwarded to any other program object. Write a simple example to accomplish what you've described, and if you have problems with it, post it and ask for help.

    Well sure, this is how my whole code works, it just sends code to active window. Now if anyone would be able to make this code be sent to notepad (for example) while its in the background it would blow my mind.

    	    public static void main(String[] args) throws AWTException,IOException {
     
    			    Robot robot = new Robot();
     
    			  robot.keyPress(KeyEvent.VK_N);
    			  robot.delay(100);
    			  robot.keyRelease(KeyEvent.VK_N);
     
    			}

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Is it possible to send keystrokes to a background window?

    Quote Originally Posted by spirates View Post
    Now if anyone would be able to make this code be sent to notepad (for example) while its in the background it would blow my mind.
    Figure out where notepad is, then send a click command to bring it to the front before sending your key commands.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,112
    Thanks
    65
    Thanked 2,718 Times in 2,668 Posts

    Default Re: Is it possible to send keystrokes to a background window?

    while its in the background
    What does "in the background" mean? If the program's window is shown on the computer's monitor, Robot can position the cursor over the window and send keystrokes to it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 9
    Last Post: January 23rd, 2014, 11:28 AM
  2. Java 2D Game: Implementing Simultaneous Keystrokes
    By AvivC in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 1st, 2014, 02:35 PM
  3. JPanel not reacting to Keystrokes
    By sonicjr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 17th, 2012, 07:10 PM
  4. Replies: 1
    Last Post: December 17th, 2011, 03:32 AM
  5. Regarding Keystrokes
    By esplanade56 in forum Java Theory & Questions
    Replies: 12
    Last Post: May 25th, 2011, 06:46 AM