Java中如何识别按键事件
我在机器人对象上使用键盘事件......
但每次我都必须单独指定键......就像
Robot r=new Robot();
r.KeyPress(KeyEvent.VK_A);
r.KeyPress(KeyEvent.VK_B);
r.KeyPress(KeyEvent.VK_C);
r.KeyPress(KeyEvent.VK_D);
有什么技术来获取/识别每个键......而不是通过单独指定它们...... .? 我从服务器端在 keyCode 变量中接收键码...... 那么我可以直接使用这个变量来代替 "KeyEvent.VK_D" 就像 r.keyPress(keyCode);
i m using the keyboard event on Robot Objects....
but each time i have to specify the keys individually....like
Robot r=new Robot();
r.KeyPress(KeyEvent.VK_A);
r.KeyPress(KeyEvent.VK_B);
r.KeyPress(KeyEvent.VK_C);
r.KeyPress(KeyEvent.VK_D);
is there any technique to get/recognize eachand every keys....not by specifying them individually....?
i m recieving the keycode from server side in keyCode variable....
so can i use this variable directly inplace of "KeyEvent.VK_D" like r.keyPress(keyCode);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Key 标识符只是一个 Int 值。 当服务器值与java值匹配时,您可以直接将值放入。如果不匹配,您必须创建一个Map,其中服务器值引用java键值。
我在 C++ Qt 按键事件和 Java 按键事件之间转换时遇到了同样的问题。 该值也不匹配。 我必须为此创建一个映射。
看一下 KeyEvent 类。 每个键都列出了一个 int 值。 您必须检查从服务器获取的值是否与该 int 值匹配。 当服务器值不匹配时,您必须创建一个映射。 映射键是服务器值,映射值是按下的键对应的 KeyEvent 值。
当值匹配时,您不必创建映射。 您可以直接使用 Robot 命令的服务器值。
The Key identifier is just an Int value. When the server value matches the java value, than you an directly put the value in. If not you have to create a Map where the server value references to the java key value.
I had the same issue during converstion between C++ Qt key events and Java Key events. The value also does not match. I had to create a mapping for this.
Take a look at the class KeyEvent. Every Key is listed their with a int value. You have to check if the value you get from the server matches with this int value. When the server value is not matching you have to create a Map. The map key is the server value and the map value is the corresponding KeyEvent valuke for the pressed key.
When the values matching, you dont have to create a map. You directly can use the server value for the Robot command.
嗯...是吗? KeyEvent.VK_D 中的常量只是为了您的方便而存在。 使用其他地方的数值绝对没有问题,只要相同的值用于相同的键即可。
Um... yes? The constants in KeyEvent.VK_D are merely there for your convenience. There is absolutely nothing wrong with using numerical values from somewhere else, as long as the same values are used for the same keys.
如果我正确理解你的问题,那么你试图在按下任何键时执行此操作。 尽管我怀疑我是否正确阅读了您的问题,但解决方案是使用 KeyEvent.KEY_PRESSED,如果您想要执行操作或在释放按键时设置变量,则为 KeyEvent.KEY_RELEASED。 虽然,我有点困惑。 您是否真的希望按下每个键时都发生相同的操作,或者您希望一组键有一个唯一的事件?
If I understand your question correctly, you're trying to get this action to take place whenever any key is pushed. Although I doubt I'm reading your question correctly, the solution to this would be to use KeyEvent.KEY_PRESSED and if you want an action or to set a variable when a key is released it would be KeyEvent.KEY_RELEASED. Although, I am a bit confused. Do you really want the same action to occur when every key is pressed or do you want a unique event for a set of keys?