简体   繁体   中英

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

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 KeyEvent s 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM