Results 1 to 10 of 10
- 01-29-2012, 12:58 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 10
- Rep Power
- 0
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; } } /*}*/);
-
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 */
- 01-29-2012, 01:22 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Problem with using the keyboard - completely stumped
The keyboard code is commented out and, as posted, it won't compile.the keyboard code does absolutely nothing.
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 01:27 AM. Reason: slow :( - got interrupted while posting
- 01-29-2012, 01:56 AM #4
Member
- Join Date
- Nov 2011
- Posts
- 10
- Rep Power
- 0
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 02:00 AM.
- 01-29-2012, 02:06 AM #5
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?
- 01-29-2012, 02:21 AM #6
Member
- Join Date
- Nov 2011
- Posts
- 10
- Rep Power
- 0
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/1iVtgpFHdBpuPtUt9t4uQ4Last edited by cwbr; 01-29-2012 at 02:25 AM.
- 01-29-2012, 02:38 AM #7
Re: Problem with using the keyboard - completely stumped
Does the component with the listener have the focus?the KeyListener isn't being called. I still can't think why though.
- 01-29-2012, 02:45 AM #8
Member
- Join Date
- Nov 2011
- Posts
- 10
- Rep Power
- 0
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 JPanelLast edited by cwbr; 01-29-2012 at 02:59 AM.
-
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 03:03 AM.
- 01-29-2012, 03:03 AM #10
Member
- Join Date
- Nov 2011
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Stumped... any help would be appreciated!
By gard007 in forum New To JavaReplies: 3Last Post: 12-01-2011, 07:42 AM -
Problem using KeyAdapter to get keyboard input
By DerekRaimann in forum New To JavaReplies: 0Last Post: 12-20-2010, 04:10 AM -
stumped......
By trueblue in forum New To JavaReplies: 21Last Post: 07-13-2009, 04:16 PM -
Completely new to Java problem
By Seamo14 in forum New To JavaReplies: 4Last Post: 10-09-2008, 12:50 PM -
KeyBoard Problem
By arimakidd in forum Java AppletsReplies: 3Last Post: 09-19-2008, 04:05 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks