简体   繁体   English

如何判断按下哪个SHIFT键?

[英]How to tell which SHIFT key was pressed?

In my game, I want to be able to use the right and left shift keys for different functions. 在我的游戏中,我希望能够使用右移和左移键来实现不同的功能。 In Java (or another language), is there any way to distinguish between those two? 在Java(或其他语言)中,有没有办法区分这两者?

The KeyEvent class has only VK_SHIFT, which corresponds to both left and right shift keys. KeyEvent类只有VK_SHIFT,它对应左右Shift键。 Same with Control , Alt , Enter , etc. ControlAltEnter等相同

My primary concern is someone may be able to use two fingers to rapidly press both keys at the same time, gaining an unfair advantage. 我主要担心的是有人可能会用两根手指同时快速按下两个按键,从而获得不公平的优势。 Should I be concerned about this? 我应该关注这件事吗?

I found a Java tutorial that includes a Java WebStart sample and the source code. 我找到了一个包含Java WebStart示例和源代码的Java教程。 Looks like the winner is KeyEvent.getKeyLocation() 看起来赢家是KeyEvent.getKeyLocation()

  • KeyEvent.KEY_LOCATION_STANDARD KeyEvent.KEY_LOCATION_STANDARD
  • KeyEvent.KEY_LOCATION_LEFT KeyEvent.KEY_LOCATION_LEFT
  • KeyEvent.KEY_LOCATION_RIGHT KeyEvent.KEY_LOCATION_RIGHT
  • KeyEvent.KEY_LOCATION_NUMPAD KeyEvent.KEY_LOCATION_NUMPAD
  • KeyEvent.KEY_LOCATION_UNKNOWN KeyEvent.KEY_LOCATION_UNKNOWN

References: 参考文献:

Key Listener Demo and Source Code 关键监听器演示和源代码

KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar, int keyLocation) 

Method: 方法:

 int    getKeyLocation()

Returns the location of the key that originated this key event. 返回发起此键事件的键的位置。

public static final int KEY_LOCATION_LEFT

A constant indicating that the key pressed or released is in the left key location (there is more than one possible location for this key). Example: the left shift key.

You can also have a look at jinput, a library that gives you direct access to all input devices, giving you a lot more control. 您还可以查看jinput,这是一个可以直接访问所有输入设备的库,为您提供更多控制。 If you decided to use LWJGL for your game, it comes with jinput already. 如果你决定在你的游戏中使用LWJGL,它已经带有jinput。

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

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