简体   繁体   English

键盘事件

[英]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... 我已经添加了键盘事件...。使用机器人类的对象将键值写在记事本上.....但是当我按“ a”键时 ,它将解释为“ 1” ,依此类推...对于所有键...

when im displaying the asci values for all the keys it will print accurate values like a-97,b-98 and so on.... 当我显示所有按键的asci值时,它将打印准确的值,例如a-97,b-98等。

why this happened please give some solution.... 为什么会发生这种情况,请提供一些解决方案。

The Robot.keyPress method takes in an int key code -- not an actual character code. Robot.keyPress方法采用int键代码-而不是实际的字符代码。

From the documentation regarding keycode : 从有关键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 : 为了正常工作,必须使用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. 另外,如果一个人想使用KeyEvent期从一个返回KeyListener的事件,如keyPressedkeyReleased ,该KeyEvent对象有一个getKeyCode方法,该方法将返回keycode事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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