Solved

Detecting F keys

Posted on 2014-01-31
15
296 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now