Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Norm

Thread: Key Listener

  1. #1
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default Key Listener

    Hello everyone,
    I'm trying to add a Key Listener that will "listen" to a single key when it pressed on a GUI.
    I looked for guides and all of them adding the key listener to an instance such as TextArea1.addKey~~
    I want they key Listener to work even if the Jar file is minimized and I'm doing somthing else and not only when i'm on a specific textare or somthing.

    Any help will be appreciated.

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

    Default Re: Key Listener

    Java can't detect key events when it does not have the focus.

  3. #3
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default Re: Key Listener


    So if I'm focused on the Jar window and I want to add a key Listener to a GUI swing for example how would I do that?
    Again, all the tutorials showed that the key listener is attached to an instance.. I don't want that .. I want the whole window!

    Thanks.

    edit: hmmm waitt!! maybe to the panel? I wish I could have now a jde to test it.

    edit2: I tried to add a Key Listener to the frame and nothing happend and also to the frame.
    Last edited by tnrh1; 11-22-2011 at 01:47 PM.

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

    Default Re: Key Listener

    I want the whole window!
    What class represents the whole window?

  5. #5
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default Re: Key Listener

    The JFrame class.
    The class sign looks like this: public class ClassName extends JFrame implements KeyListener

    And I added this code:
    addKeyListener(this);

    And the unimplemented methods keyTyped etc ..

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

    Default Re: Key Listener

    Have you looked at Key Binding? That can be easier to use and more reliable.
    Fubarable likes this.

  7. #7
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

Similar Threads

  1. key listener
    By nikkka in forum New To Java
    Replies: 8
    Last Post: 04-23-2011, 09:36 AM
  2. J2ME sms listener
    By bharani in forum CLDC and MIDP
    Replies: 4
    Last Post: 11-21-2010, 06:33 PM
  3. Tab Listener
    By teckno101 in forum AWT / Swing
    Replies: 2
    Last Post: 09-29-2009, 09:40 PM
  4. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-20-2008, 11:07 PM
  5. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-10-2008, 02:00 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
  •