Results 1 to 17 of 17
  1. #1
    gen1mx6 is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default get keyboard input while running in the background?

    my goal for this program is to run in the background, and when a certain key (or combination) is pushed my program executes a thread (in my case robot.keypress)

    its like a macro program i just have no clue how to listen for a key while the actual program itself isn't in focus.

  2. #2
    gen1mx6 is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    its probably some api function i don't know... so fwiw i'm using vista

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Anyway you need to have an implementation to listen key-events and must have a way to communicate with the main application.

    I think you are looking for key listeners.

    How to Write a Key Listener (The Java™ Tutorials > Creating a GUI with JFC/Swing > Writing Event Listeners)

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    But if he is running in the background, unless it's some sort of plugin he won't get KeyEvents. I can't think of any way to get key-presses without being part of the actual program.

    @OP I don't believe there is really any good way of doing this in java. Only thing I could think of would be changing it to something like a command line.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    That's what I'm trying to point here Boyo. Must need a way to communicate with the application, background application.

  6. #6
    Waqas is offline Member
    Join Date
    Jan 2009
    Posts
    10
    Rep Power
    0

    Default

    whatever u advised doesnt seem to meet the actual prob .........

  7. #7
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Java is the wrong tool for this task. You need a native language that can hook into the OS key handler chain.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Waqas View Post
    whatever u advised doesnt seem to meet the actual prob .........
    Then better give your solution here.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by dlorde View Post
    Java is the wrong tool for this task. You need a native language that can hook into the OS key handler chain.
    Can you explain a bit, on what contest you say that, Java is not suitable for this?

  10. #10
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Hmm, it's the same thing I pointed out. Java has no way of listening for key-presses in other applications. C++, C, and other languages are far more likely to able to link into the OS key handlers, partly because they generate .exe files that can be run by any OS. Because Java is run in its own Virtual Machine (the JVM) it doesn't have any links to other programs, as it is not directly integrated with the OS, which, while likely making it more portable, it does make it extremely difficult, if not impossible, to write programs that would require direct integration with the OS.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Wait wait, now I'm confusing. I've to go the OPs' question again. Is his background program is not a Java program? I'm mess-up with that.

  12. #12
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    What the OP really wants is some kind of 'daemon' program that will constantly wait in the background, listen for key-presses, and react accordingly. But Java doesn't do daemon programming at all...
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes lol, I got the point now. Actually I've miss-understood the question. I'm really sorry about that, I'll try to give a better explanation on my thoughts of the original question.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    It's simple. You can't write a keylogger in Java.

    db

  15. #15
    chiragkini is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default KeyLogger

    Hey guys,
    You can use robot class and can use setVisible(false) to work in background.......

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by chiragkini View Post
    Hey guys,
    You can use robot class and can use setVisible(false) to work in background.......
    How will this read in keystrokes if the GUI does not have focus?

  17. #17
    chiragkini is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default KeyLogger .....

    hi Fubarable,
    thanks for your interest. Now what u want to achieve can be done using java Robot class and JFrame class for its gui. Now how to do it, for your help i can only say that search for Eboook- "Java After Hours:10 Projects You'll Never Do at Work" by -- Steven Holzner
    In that book browse the section ' Running Any Program Via Remote Control with the Robot' this will definetly help you to achieve what you want , just you need to implement your logic to get keystrokes.......

    I dont have link for ebook buut i am sure you will get it by little bit googling...
    If you dont get the link then send me request for same on my "chiragkini" gmail account i will send you that ebook..


    Hope u succeed!
    Best of Luck!!!!!;)
    Last edited by chiragkini; 07-16-2009 at 04:56 PM.

Similar Threads

  1. getting each character from keyboard
    By Sreejesh25 in forum Advanced Java
    Replies: 6
    Last Post: 03-05-2010, 08:12 PM
  2. KeyBoard Problem
    By arimakidd in forum Java Applets
    Replies: 3
    Last Post: 09-19-2008, 05:05 PM
  3. Basic keyboard input and edit
    By BHCluster in forum New To Java
    Replies: 18
    Last Post: 08-09-2008, 12:52 AM
  4. Polled keyboard input through swing
    By Prometheus in forum Advanced Java
    Replies: 2
    Last Post: 02-04-2008, 05:05 PM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 09:46 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
  •