[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 618
  • Last Modified:

compare arrow key in java applet code

User press a key and the key press event get called from the event handler i am calling another function where i am passing the   char c

public void keyPressed(KeyEvent arg0) {
  char c = e.getKeyChar();
  handleKeyPressedOrTyped(c);
}
_____________________________________________________
public void handleKeyPressedOrTyped(char c) {

// here i have compare the char with left and right arrow key    
// if ( c is left or right arrow key) then do something
 }

i dont know how to compare a char with left or right arrow key??????????
please help :(



public void keyPressed(KeyEvent arg0) {
  char c = e.getKeyChar();
  handleKeyPressedOrTyped(c);
}
_____________________________________________________
public void handleKeyPressedOrTyped(char c) { 
 
// here i have compare the char with left and right arrow key     
// if ( c is left or right arrow key) then do something
 }

Open in new window

0
PoorDude
Asked:
PoorDude
  • 2
1 Solution
 
Thomas4019Commented:
Use e.getKeyCode() and KeyEvent.VK_LEFT
Look at this page for the full list of keycodes.

http://java.sun.com/javase/6/docs/api/java/awt/event/KeyEvent.html
public void keyPressed(KeyEvent e) 
{
  int c = e.getKeyCode();
  handleKeyPressedOrTyped(c);
}
 
public void handleKeyPressedOrTyped(int c)
{
	if(c == KeyEvent.VK_LEFT)
	{
		//Left arrow key was pressed
	}
	else if(c == KeyEvent.VK_UP)
	{
		//Up arrow key was pressed
	}
	else if(c == KeyEvent.VK_1)
	{
		//The key "1" was pressed
	}
	else if(c == KeyEvent.VK_A)
	{
		//The "a" key was pressed
	}
}

Open in new window

0
 
PoorDudeAuthor Commented:
I can't change the function signature of handleKeyPressedOrTyped and i canot change the keyPressed event also.....
I CAN CHANGE ONLY THE CODE OF handleKeyPressedOrTyped FUNCTION.

public void keyPressed(KeyEvent arg0) {
  char c = e.getKeyChar();
  handleKeyPressedOrTyped(c);
}

public void handleKeyPressedOrTyped(char c) {
 
// here i have compare the char with left and right arrow key    
// if ( c is left or right arrow key) then do something
 }
 
0
 
Thomas4019Commented:
"The getKeyChar method always returns a valid Unicode character or CHAR_UNDEFINED."
http://java.sun.com/javase/6/docs/api/java/awt/event/KeyEvent.html

Well, there really isnt a character to represent the arrows keys and many other keys on the key board. So Java, with these keys returns the same char signifing an unrepresentable key press. For example all the arrow keys, the home key, page up, page down all give a char of integer equivalent 65535 which is the constant CHAR_UNDEFINED.

So basically if you can only use the getKeyChar() you cannot determine when arrow keys are pressed. It can be done quite easily with getKeyCode() however.
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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now