Link to home
Start Free TrialLog in
Avatar of sargent240
sargent240Flag for United States of America

asked on

Detecting F keys

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);
       
    }
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Use a Keylistener. VK number constants.
Avatar of sargent240

ASKER

Boy, I have no idea what you are talking about.  Can you give me a line or two of code?
Thanks
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.
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?
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

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!
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Again, thank you!
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.
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
@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.
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!
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.