Hi
I'm trying to implement a keylistener to my application.
The page contains six JButtons to start different part of the
application - that works.
I want to implement a key shortcut to each part but I can't get i to
work.
I've tried to add the keylistener to the frame, the panel and the
button - eact time without luck.
In my Keylistener i write:
public void keyTyped(KeyEvent event) {
if(event.getKeyCode()==KeyEvent.VK_F1)
System.out.println("test");
}
What am I doing worng - and can someone give me an example?
Thanks
Brian
Jeff Higgins - 18 Nov 2007 20:19 GMT
> Hi
>
[quoted text clipped - 8 lines]
> I've tried to add the keylistener to the frame, the panel and the
> button - eact time without luck.
Why not try using the
JButton.setMnemonic(KeyEvent.VK_F1);
and an ActionListener instead?
JButton.setActionCommand("help");
> In my Keylistener i write:
> public void keyTyped(KeyEvent event) {
[quoted text clipped - 3 lines]
>
> What am I doing worng - and can someone give me an example?
<http://java.sun.com/docs/books/tutorial/uiswing/components/button.html#jbutton>
> Thanks
> Brian
Joshua Cranmer - 18 Nov 2007 20:26 GMT
> I want to implement a key shortcut to each part but I can't get i to
> work.
[quoted text clipped - 9 lines]
>
> What am I doing worng - and can someone give me an example?
Your code looks fine to me on an eyeball-inspection. Almost definitely,
the problem is in the code you haven't shown us (it most often is).

Signature
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
Roedy Green - 18 Nov 2007 20:37 GMT
>What am I doing worng - and can someone give me an example?
see http://mindprod.com/jgloss/event11.html
http://mindprod.com/products2.html#KEYPLAYER
http://mindprod.com/jgloss/sscce.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Brian - 21 Nov 2007 13:39 GMT
>In my Keylistener i write:
>public void keyTyped(KeyEvent event) {
>if(event.getKeyCode()==KeyEvent.VK_F1)
>System.out.println("test");
>}
Problem solved :)
I found that i couldn't use 'keyTyped' with F1 keys - so I use
'keyPressed' instead
Thanks for your help :)
Brian