Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Detecting F keys

Posted on 2014-01-31
15
Medium Priority
?
311 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 17

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 17

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 17

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 36

Accepted Solution

by:
mccarl earned 2000 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 17

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 17

Expert Comment

by:krakatoa
ID: 39833639
0
 
LVL 36

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 17

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 17

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

824 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