Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Help using Keylistener

Thread view: 
Brian - 18 Nov 2007 19:28 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.