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 5 of 5

Thread: Add keyListener to thread

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Add keyListener to thread

    Hello,

    I want to try and run a thread that starts running on start and quits after pressing a key.
    I cant seem to detect keypresses. can anyone help me out with this?

    Thanks in advance..
    i cant seem to add code:
    Post denied. New posts are limited by number of URLs it may contain and checked if it doesn't contain forbidden words.

    its just a code snippit without links or anything...

    this is a small part... but how do i add a keyListener in here that listends to a random key?
    The class implements KeyListener and the overrided method "keyPressed" sets the isRunning boolean to false... but the keyPressed method is never executed.
    public static void main(String[] args) {
    		Thread thread = new Thread() {
    			public void run() {
     
    				while(isRunning){
    					System.out.println("running");
    					try {
    						sleep(1000);
    					} catch (InterruptedException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    			    }
    			}
    		};
    		thread.start();
    	}


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

    Default Re: Add keyListener to thread

    This is a console program, not a GUI, right?

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Add keyListener to thread

    actually i was hoping to be able to do it with just a thread in the background but i dont think it's possible...
    I want a background thread to keep track of key presses and kill the thread after pressing a key like esc or something...

    Like a keylogger. but for a different purpose

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Add keyListener to thread

    Quote Originally Posted by Qlubbie View Post
    actually i was hoping to be able to do it with just a thread in the background
    Listeners, in general, are used with GUI (AWT/Swing) components ..... not for any non-GUI application.

    Quote Originally Posted by Qlubbie View Post
    I want a background thread to keep track of key presses and kill the thread after pressing a key like esc or something...

    Like a keylogger. but for a different purpose
    If you want to receive keyboard events "globally" on the system, even if the user is interacting with any other application, beware that this is not possible with the standard Java SE framework.
    For these things, you need to know and use some native O.S. functions (if there are, and at least for the Windows platform, the Win32 API contains these functions). And from Java you need to use JNI, Java Native Interface to use these functions. All these are advanced things, that are very specific for each O.S.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

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

    Default Re: Add keyListener to thread

    Closing this thread as either 'solved' or as a suspicious activity prohibited by the forum.

Similar Threads

  1. KeyListener
    By tim8w in forum AWT / Java Swing
    Replies: 9
    Last Post: January 25th, 2013, 01:40 AM
  2. Error on KeyListener
    By Colino in forum AWT / Java Swing
    Replies: 12
    Last Post: January 14th, 2012, 03:58 AM
  3. help with KeyListener
    By all_pro in forum AWT / Java Swing
    Replies: 4
    Last Post: April 14th, 2011, 06:51 AM
  4. My KeyListener is not Working!!
    By DarrenReeder in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2010, 05:18 PM
  5. Problem with KeyListener
    By r12ki in forum AWT / Java Swing
    Replies: 3
    Last Post: October 1st, 2009, 01:18 PM