Help using Keylistener

B

Brian

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
 
J

Jeff Higgins

Brian said:
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.

Why not try using the
JButton.setMnemonic(KeyEvent.VK_F1);
and an ActionListener instead?
JButton.setActionCommand("help");
 
J

Joshua Cranmer

Brian said:
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?

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).
 
B

Brian

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top