gajera sanjay
gajera sanjay

Reputation:

keyboard event

i have added keyboard events....with robot class' object to write the key values on the notepad.....but as i press "a" it will interpret it as "1" and as so on...for all the keys...

when i m displaying the asci values for all the keys it will print accurate values like a-97,b-98 and so on....

why this happened please give some solution....

Upvotes: 0

Views: 337

Answers (1)

coobird
coobird

Reputation: 160974

The Robot.keyPress method takes in an int key code -- not an actual character code.

From the documentation regarding keycode:

keycode - Key to press (e.g. KeyEvent.VK_A) 

Therefore, entering the following will not work:

Robot r = new Robot();
r.keyPress('a');            // Won't work -- it will press an "1"

To work correctly, one would have to use the constants from KeyEvent:

Robot r = new Robot();
r.keyPress(KeyEvent.VK_A);  // This works -- it will press an "a"

Also, if one wants to use the KeyEvents returned from a KeyListener's events such as keyPressed and keyReleased, the KeyEvent object has a getKeyCode method which will return the keycode of the event.

Upvotes: 2

Related Questions