Solved

Detecting F keys

Posted on 2014-01-31
15
305 Views
Last Modified: 2014-02-05
following is code I am using to try to detect when a user presses an F key.  It detects all the keys except the F keys.  I am using netbeans.


    private void jTabbedPane1KeyTyped(java.awt.event.KeyEvent evt) {

               char c = evt.getKeyChar();
              int keyCode = evt.getKeyCode();
              int modifiers = evt.getModifiers();
              String tmpString = KeyEvent.getKeyModifiersText(modifiers);
       
    }
0
Comment
Question by:sargent240
  • 7
  • 6
  • 2
15 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 39825164
Use a Keylistener. VK number constants.
0
 

Author Comment

by:sargent240
ID: 39825207
Boy, I have no idea what you are talking about.  Can you give me a line or two of code?
Thanks
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39825223
Well, for a start, I suppose you have run through the doc on KeyEvent . . . ? -

http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html

. . .  which inter alia describes key static ints for these, eg

static int      VK_F1 Constant for the F1 function key.


IOW, you need a KeyListener of some kind to look out for the key. A KeyAdapter may be the cheapest way to get into it.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sargent240
ID: 39825892
Using the code I posted in my first posting I get all the info except the info when an F key is pressed.  I am not familiar with how to determine if an F key has been pressed.  Can I use a simple line like I do to get c, or the keyCode, or modifiers, or tmpString?
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39826319
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;



class KeyReact extends JFrame{


	public static void main(String[] args){


		JFrame kR = new KeyReact();

		JTextArea jTA = new JTextArea();

		kR.add(jTA);

		kR.setSize(new Dimension(150,75));

		kR.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		kR.setVisible(true);

		jTA.addKeyListener(new kAd());

		jTA.grabFocus();

		
	}

         static  class kAd extends KeyAdapter {
		
		public kAd(){		
			
		}	
		
		public void keyPressed(KeyEvent k){
			System.out.println(KeyEvent.getKeyText(k.getKeyCode()));
			if(k.getKeyCode()==112){System.out.println("F1 pressed");}
                        if(k.getKeyCode()==KeyEvent.VK_F1){System.out.println(k.getKeyText(k.getKeyCode()) +" pressed");}
		}
		
	}

}

Open in new window

0
 

Author Comment

by:sargent240
ID: 39828782
Thank you for the code.  I compiled it and it ran as expected.  I am using Netbeans to create my panels and panes.  I right clicked on the tabbedPanel and selected an event which created the code below.

    private void jTabbedPane1KeyTyped(java.awt.event.KeyEvent evt) {
       
    }

I THEN ADDED CODE SO IT LOOKS LIKE THE CODE BELOW.

    private void jTabbedPane1KeyTyped(java.awt.event.KeyEvent evt) {

               char c = evt.getKeyChar();
              int keyCode = evt.getKeyCode();
              int modifiers = evt.getModifiers();
              String tmpString = KeyEvent.getKeyModifiersText(modifiers);
       
    }

 WHEN I EXECUTE THE ABOVE CODE PRESSING THE a KEY AND THE A KEY, I GET THE FOLLOWING RESULTS.

char = a keycode = 0 tmpString =
char = A keycode = 0 tmpString = Shift

WHEN I PRESS AN F KEY I GET NO RESULTS AT ALL.

Is there a way I can modify my code above or ad a few lines to use the event I have created in Netbeans?

THANKS, I appreciate your help!
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39828796
I right clicked on the tabbedPanel and selected an event which created the code below.
You haven't stated here but I assume (from the code generated and the behaviour you see) that you selected the "keyTyped" event in Netbeans. You need to select either the "keyPressed" or "keyReleased" event in Netbeans.

keyTyped events are only for keys that actually generate a character, like 'a' or 'A' as you have seen. Since the function keys don't actually produce a character as such, the keyTyped event is never triggered. The other two events WILL be triggered though, so use one of those!
0
 

Author Comment

by:sargent240
ID: 39832832
The solution to my problem was in your last posting.  The original code I posted works great now that I am using "keyPressed" as opposed to "keyTyped."  Thank you for your help.
0
 

Author Closing Comment

by:sargent240
ID: 39832839
Again, thank you!
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39832860
that I am using "keyPressed" as opposed to "keyTyped."

Yeah . . . that's presumably forgetting that I had already provided that fix for you *in code* in my code snippet comment ?

The solution to my problem was in your last posting.

The solution was actually in my post.
0
 

Author Comment

by:sargent240
ID: 39833616
I doesn't make any difference at this point, when you say "in code" in my code snippet comment, I looked back through the postings and don't know what you are referring to when you say "in code in my code snipped comment.  I seem to be overlooking your code snippet comment.  Out of curiosity could you point out that snippet for me.  Thanks

Cheers
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39833639
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39834311
@sargent240,
Again, thank you!
Not a problem, glad to help! :)


@krakatoa,

While yes, your code snippet did use the "keyPressed" event, it was probably not obvious to sargent240 that that as opposed to "keyTyped" was the key difference between the original code in the question and your snippet.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39835040
While yes, your code snippet did use the "keyPressed" event, it was probably not obvious to sargent240 that that as opposed to "keyTyped" was the key difference between the original code in the question and your snippet.

. . .  then I'd expect YOU to have the courtesy and insight into the question, to have brought the asker's attention to this, and to concede that the points are not due to you on this occasion. Especially as you are involved in this TA as a volunteer!
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39835122
Yours is what's called "ungentlemanly behaviour" - and I would have expected better than that from you. It isn't just about the points - it's about this site as a help forum, and the atmosphere that is generated . . . and at the moment that atmosphere reeks of bad eggs, rather than sour grapes . . . but I'll leave it to you and your conscience.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
running on tomcat not jboss eap 7.0 3 30
Programming Codes 2 41
import as existing maven project 3 37
JDBC performance 8 29
This is an explanation of a simple data model to help parse a JSON feed
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question