Hi,
I was learning about capturing key presses via the web and I added it to one of my existing Test classes... the problem is it just does not work :(
I have added color to the parts I think are important.

In the end I want to capture the keyboard input of the numbers 1 to 7, but right now the first step is to make it damn give me a system.out.println() for every key pressed or release and I cant get that :((

Where did I screwup?

02-19-2011, 04:49 AM

Fubarable

Quote:

Where did I screwup?

For one, please edit your first post and change your "quote" tags to "code" tags so we can read the code.

02-19-2011, 04:54 AM

N00Bie

Done :))...

02-19-2011, 05:06 AM

Fubarable

To use a KeyListener, the component doing the listening must have the focus. You've added your listener to the contentPane which likely does not have focus. What is the goal of your key listening anyway? What are you trying to achieve with this?

02-19-2011, 05:13 AM

N00Bie

I'm still very much in the path of learning/(refreshing my knowledge) of Java.
So instead of just doing the examples in the tutorials I am also making a side project for myself where I can practice what i have learned because I belive the best way of learning is to create something.

So I have created a little game (I play the guitar) where a music note is displayed on the screen and I have to click one of 7 buttons to say which note it is. This works perfectly.
But moving the mouse fast enough sometimes is a problem (I have a thread running with a timer that gives me only 2 seconds to click the right button before the music note changes) so once I run the program I want to be able to use the number pad as well.

Sorry for the long explanation.

02-19-2011, 05:21 AM

Fubarable

Quote:

Sorry for the long explanation.

That's quite alright as it gives us some useful information on your problem. You may wish to explore using key bindings instead of a key listener, and you can find more about it here: how to use key bindings

It may seem more daunting at first, but it has power and flexibility including being usable even if the component doing the binding doesn't have focus.

And the Test class which I posted here is exactly that, a test class. Once things are working in the test class I make the changes in the other program that way I wont screw up something thats already working and spend time searching and undoing stuff.

02-19-2011, 05:53 AM

Fubarable

Also: If you don't mind using alt-key combinations, you could simply set the JButton's mnemonic to be alt-your number.

02-19-2011, 05:56 AM

N00Bie

That key bindings thing is pretty long with no quick examples :(
I did find this though:

adding this

Quote:

tt.requestFocusInWindow();

under my

Quote:

Test tt = new Test();
tt.setVisible(true);

seems to work for the most part but is also giving me some other output...

For keyReleased() it gives me:

Quote:

Unknown keyCode: 0x0

02-19-2011, 05:59 AM

N00Bie

Quote:

Originally Posted by Fubarable

Also: If you don't mind using alt-key combinations, you could simply set the JButton's mnemonic to be alt-your number.

AH!
Thats a very good idea, will try that as well!

02-19-2011, 06:01 AM

N00Bie

Ok, just tried the "mnemonic " way.
It works good (I don't mind the ALT) but when I try using the number pad... no joy :(

02-19-2011, 06:03 AM

Fubarable

Hm, let's see your code. I unfortunately don't have a number pad on my computer, but this worked for my laptop:

// condition tells the input map how to handle the focus. when in focused
// window allows the input map to respond if the component with bindings
// is in a window that has focus. The component itself doesn't have to have
// focus.
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = btn.getInputMap(condition);
ActionMap actionMap = btn.getActionMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_0 + count, 0), text);
//!! actionMap.put(text, action); // if you don't want the button to click

// if you do want the button to click, use code below
actionMap.put(text, new AbstractAction() {
public void actionPerformed(ActionEvent e) {
btn.doClick();
}
});