Solved

Detecting F keys

Posted on 2014-01-31
15
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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 36

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 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 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Progress

617 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