aspose file tools
The moose likes Beginning Java and the fly likes keyListener Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "keyListener" Watch "keyListener" New topic
Author

keyListener

r bal
Greenhorn

Joined: Mar 31, 2005
Posts: 6
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

Joined: Apr 21, 2005
Posts: 410
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

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Apr 21, 2005
Posts: 410
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

Joined: Mar 31, 2005
Posts: 6
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

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Mar 31, 2005
Posts: 6
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

Joined: Apr 21, 2005
Posts: 410
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

Joined: Mar 31, 2005
Posts: 6
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

Joined: Apr 21, 2005
Posts: 410
Maybe you can post the full code if it is relatively short?
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: keyListener
 
Similar Threads
addKeyListener() help needed plz
Diff Between Applet and JApplet??
Questin on listeners
Using DocumentFilter
swing & keylistener problems