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

Thread: Problem with using the keyboard - completely stumped

  1. #1
    cwbr is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Problem with using the keyboard - completely stumped

    Can anyone tell me why the mouse one works, and the keyboard one doesn't (with or without quotes)? I cannot figure it out, the keyboard code here worked until I tried to do custom painting properly.
    Anyway, the mouse code works, but the keyboard code does absolutely nothing.

    Java Code:
    addMouseListener(new MouseAdapter() {
    	public void mousePressed(MouseEvent e) {
    		int button = e.getButton();
    		switch (button) {
    		case MouseEvent.BUTTON1:
    			pc.moveUp();
    			repaint();
    			break;
    		case MouseEvent.BUTTON3:
    			pc.moveDown();
    			repaint();
    			break;
    		}
    	}
    });
    addKeyListener(new KeyAdapter() {
    	public void keyPressed(KeyEvent k) {
    		/*int keyCode = k.getKeyCode();
    		switch (keyCode) {
    		case KeyEvent.VK_LEFT:
    		case KeyEvent.VK_A:
    			pc.moveLeft();
    			repaint();
    			break;
    		case KeyEvent.VK_RIGHT:
    		case KeyEvent.VK_D:
    			pc.moveRight();
    			repaint();
    			break;
    		case KeyEvent.VK_DOWN:
    		case KeyEvent.VK_S:
    			pc.moveDown();
    			repaint();
    			break;
    		case KeyEvent.VK_UP:
    		case KeyEvent.VK_W:*/
    			pc.moveUp();
    			repaint();
    			//break;
    		}
    	}
    /*}*/);

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

    Default Re: Problem with using the keyboard - completely stumped

    Well, one reason your KeyListener does nothing because it's coded that way. It's essentially little more than a shell class filled with one large

    /* comment */

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

    Default Re: Problem with using the keyboard - completely stumped

    the keyboard code does absolutely nothing.
    The keyboard code is commented out and, as posted, it won't compile.

    Rather than "works"/"doesn't work" it would be better to post an SSCCE - that is, something brief that compiles and runs together with a brief description of the actual and intended behaviour of the code.

    [Edit] Sorry, it does compile - I got confused by the fact that the forum software doesn't syntax colour comments properly.
    Last edited by pbrockway2; 01-29-2012 at 02:27 AM. Reason: slow :( - got interrupted while posting

  4. #4
    cwbr is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: Problem with using the keyboard - completely stumped

    I'm afraid a concise snippet of code isn't possible... for the method to do anything, at least three different classes and a few small images are needed.
    I don't suppose you can think of anything that would make those two Listeners behave differently off the top of your head? The separate actions for the mouse buttons can be ignored, originally it had the same code as the keyboard one.

    Apart from the fact that one is for a keyboard and one is for a mouse, they are exactly the same, so I don't know what could be preventing it from working.

    Edit: I could provide the main .java file for a lookover, it's not too long, and everything outside it is guarateed to be the same for the mouse and the keyboard anyway.
    Last edited by cwbr; 01-29-2012 at 03:00 AM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,776
    Rep Power
    25

    Default Re: Problem with using the keyboard - completely stumped

    Is the problem that the keyListener methods are not being called? Add a println to verify it is being called by printing out a message when it is called.
    Or is the problem that the methods that the keyListener calls are not working as you want?
    pbrockway2 likes this.

  6. #6
    cwbr is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: Problem with using the keyboard - completely stumped

    Thanks for the suggestion, I tested it, and the KeyListener isn't being called. I still can't think why though.

    Edit: If anyone would be willing to look over the code, I have a shortened version here: http://ubuntuone.com/1iVtgpFHdBpuPtUt9t4uQ4
    Last edited by cwbr; 01-29-2012 at 03:25 AM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,776
    Rep Power
    25

    Default Re: Problem with using the keyboard - completely stumped

    the KeyListener isn't being called. I still can't think why though.
    Does the component with the listener have the focus?

  8. #8
    cwbr is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: Problem with using the keyboard - completely stumped

    I don't know how you would tell. I assume it does, since the mouse listener works, but is there a test? I could check.

    Edit: I put a mouse listener and key listener on the JFrame that hold the JPanel, and neither of them register anything, so I think the focus is on the JPanel
    Last edited by cwbr; 01-29-2012 at 03:59 AM.

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

    Default Re: Problem with using the keyboard - completely stumped

    Yep. You could always use a Swing Timer to check for focus and print out the result with a println, but regardless, you shouldn't be using a KeyListener with a Swing component. Use Key Bindings and your focus issues are fixed.
    Last edited by Fubarable; 01-29-2012 at 04:03 AM.

  10. #10
    cwbr is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: Problem with using the keyboard - completely stumped

    Hmm, haven't heard of Key Bindings. Can you point me in the right direction and I'll have a look at them?

    Edit: Found them. I'll have a look and see if that solves it. Thanks, everyone
    Last edited by cwbr; 01-29-2012 at 04:06 AM.

Similar Threads

  1. Stumped... any help would be appreciated!
    By gard007 in forum New To Java
    Replies: 3
    Last Post: 12-01-2011, 08:42 AM
  2. Problem using KeyAdapter to get keyboard input
    By DerekRaimann in forum New To Java
    Replies: 0
    Last Post: 12-20-2010, 05:10 AM
  3. stumped......
    By trueblue in forum New To Java
    Replies: 21
    Last Post: 07-13-2009, 05:16 PM
  4. Completely new to Java problem
    By Seamo14 in forum New To Java
    Replies: 4
    Last Post: 10-09-2008, 01:50 PM
  5. KeyBoard Problem
    By arimakidd in forum Java Applets
    Replies: 3
    Last Post: 09-19-2008, 05:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •