Results 1 to 8 of 8
  1. #1
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default KeyListener is Not Working

    WARNING: Issue probably due to a stupid mistake.

    Java Code:
    w.b.addKeyListener(this);
    w.b.addMouseListener(this);
    Java Code:
    public void keyPressed(KeyEvent e) {
        code=e.getKeyCode();
        go();
        System.out.println("Key pressed.");
    }
    public void mousePressed(MouseEvent m) {
        go();
    }
    My MouseListener works fine. My KeyListener does not. Why?

    The code is in a controller class. w is a JFrame. b is a JPanel within the JFrame, used for painting.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: KeyListener is Not Working

    It's impossible to say with just a snippet of code.

    Perhaps the panel does not have focus as described in the How to Write a Key Listener page of Oracle's Tutorial and the focus subsystem discussion linked to from there.

    If that isn't the case, consider posting a SSCCE: brief, runnable code that illustrates the problem (but without extraneous program logic that isn't part of the problem.)
    Last edited by pbrockway2; 04-14-2013 at 04:03 AM.

  3. #3
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: KeyListener is Not Working

    Thanks. Never had to set focus before.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,935
    Rep Power
    4

    Default Re: KeyListener is Not Working

    Well, after some experimenting I have determined that using setFocusable(true) will allow my JPanel to process KeyListener events. It is not clear to me that setFocusable() should make a difference since JPanel is focusable by default and is verified by isFocusable(). But after looking at the code, setFocusable() does fire off a propertyChange event as well as altering some internal state variables. So it appears that setFocusable() also obtains the focus. This was not intuitive to me so I will defer to the explanation from others.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: KeyListener is Not Working

    Now I have my KeyListener working with a timer repositioning and repainting Mario every 20 milliseconds. That's good, but I can't use more than one key at a time. Is there a way to check which buttons are pressed in a timer? If not, how else can I solve this problem? (I have booleans for each of the keys being used. This works, but not very smoothly.)

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: KeyListener is Not Working

    (I have booleans for each of the keys being used. This works, but not very smoothly.)
    A bunch of booleans is what I'd use. (Corrections from the more knowledgeable are welcome!) Perhaps you could post some runnable code that shows the lack of smoothness.

    So if I wanted to move a character in response to 4 keys I'd have 4 booleans: left, right, up, down and the logic would be something like:

    On key up: change the appropriate boolean to false, if none of the booleans is true stop the timer
    On key down: if none of the booleans are true start the timer, change the appropriate boolean to true
    On timer clicks: check all the booleans, move the character appropriately and ask for a repaint()

  7. #7
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: KeyListener is Not Working

    Quote Originally Posted by pbrockway2 View Post
    A bunch of booleans is what I'd use. (Corrections from the more knowledgeable are welcome!) Perhaps you could post some runnable code that shows the lack of smoothness.

    So if I wanted to move a character in response to 4 keys I'd have 4 booleans: left, right, up, down and the logic would be something like:

    On key up: change the appropriate boolean to false, if none of the booleans is true stop the timer
    On key down: if none of the booleans are true start the timer, change the appropriate boolean to true
    On timer clicks: check all the booleans, move the character appropriately and ask for a repaint()
    Worked. Thanks.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. KeyListener in JTextField not working
    By AndrewM16921 in forum AWT / Swing
    Replies: 5
    Last Post: 12-11-2012, 01:48 AM
  2. Replies: 4
    Last Post: 08-17-2012, 10:38 PM
  3. KeyListener not working
    By cc11rocks in forum AWT / Swing
    Replies: 3
    Last Post: 02-09-2011, 01:09 PM
  4. keylistener + applet isn't working
    By brandon95 in forum Java Applets
    Replies: 6
    Last Post: 01-03-2011, 02:10 PM
  5. keyListener isn't working for me
    By lost_in_java in forum AWT / Swing
    Replies: 7
    Last Post: 12-05-2008, 04:24 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
  •