• Post Reply Bookmark Topic Watch Topic
  • New Topic

keyListener  RSS feed

 
r bal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For some reason my key listeners don't even register anything. Even if I had println statements in it, they do not come up.

In my class with the main method, I have have extended JFrame. In my constructor for this class I have:

addKeyListener(myBoard); Where myBoard has been created as an object of Board class.

Then in Board class I have my keyListeners. The method implements KeyListener. And I have the three methods keyPressed, keyTyped, and keyReleased.

public void keyTyped(KeyEvent keyEvent) {
int k = keyEvent.getKeyCode();
if (k == KeyEvent.VK_DOWN) {
System.out.println("down working");
}
else if (k == KeyEvent.VK_UP) {
System.out.println("up working");
}
}

public void keyPressed(KeyEvent keyEvent) {
}

public void keyReleased(KeyEvent keyEvent) {
}


Then in my constructor for this class I have:
addKeyListener(this);

Any clue as to why my keyListener is not working???
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put a System.out.println call in those three methods before any of the if statements. They should be being called.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from the KeyEvent api

"For key pressed and key released events, the getKeyCode method returns the
event's keyCode. For key typed events, the getKeyCode method always returns
VK_UNDEFINED."

change to keyPressed/Released and it should work OK
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, that is where I was heading. I figured the methods must be being called, so probably the if statements were not evaluating to true. Didn't have the API docs handy though.

r bal sometimes it can be very useful to add a few extra println calls to your code to determine what is happening. By putting a println at the top of the method, you can tell whether or not the method is being called at all. Its a very useful debugging technique sometimes.

[ May 13, 2005: Message edited by: Stuart Gray ]
 
r bal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for all the advice....

but unfortunately, the methods aren't even being read. Ie. I put println statements in all the 3 methods before the if statements, and still nothing happened.

i guess i'll keep on with the trial and error.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it could be what you're adding the listener to.

here's what I think is a reconstruction of your description,
it works OK adding it directly to the frame, and also to the panel,
if the indicated line is set.

 
r bal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The option you had that was commented out...was the one I orginally had.

I tried the other option using the JPanel, but still no success.

I have tried absolutely everything I can think of.

This has me stumped.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A thought - when you call addKeyListener(myboard), has myboard already been initialised at this point? The reason I ask is because if it is null the addKeyListener won't work - but no error message will be thrown either! Basically you should have a line like this somewhere before you try to add the KeyListener:
 
r bal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, i have that line of code before I add the keyListener.

I just can't figure it out...everything I need seems to be there and in the right place.

Is there any other way to go about adding keyListeners using different methods or different classes?
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you can post the full code if it is relatively short?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!