如何识别“Alt +”按键是否正确、可靠?
我的问题实际上更笼统,但我使用用户按住“Alt”键并按“+”的操作作为显示困难的示例。
我正在使用美国英语键盘,该键盘在同一个键上有“=”(小写)和“+”(大写),因此要按“Alt +”(如菜单条目中所示),我必须实际按下“Alt Shift =”。在 Java AWT 中,按“Alt Shift =”会生成一个按键式 KeyEvent,其中的键码与“=”键相关联,以及一个包含“±”字符的键类型式 KeyEvent。因此,没有明显、可靠的方法来以编程方式确定按下“+”键时按住“Alt”。
我可以在内部进行一些映射来解决此问题,例如将“±”映射到“Alt +”,或将“Shift {keycode for = }”映射到“+”。然而,似乎没有任何保证这可以在不同的键盘布局上工作;这当然不是好的编码风格。
如果有人可以提出解决这些问题的方法,或者指出已经解决了这个困难的代码,我将不胜感激。
谢谢。
My question is actually more general, but I'm using the action of a user holding down the "Alt" key, and pressing "+", as an example that shows the difficulties.
I'm working an a US English keyboard, which has the "=" (lowercase) and "+" (uppercase) on the same key, so to press "Alt +" (as might be indicated in a menu entry), I have to actually press "Alt Shift =". In Java AWT, pressing "Alt Shift =" generates a key-pressed KeyEvent with the keycode associated with the "=" key, and a key-typed KeyEvent containing the "±" character. So there is no obvious, reliable way of programatically deciding that "Alt" was held down while the "+" key was pressed.
I could do some mapping internally to fix this, such as mapping "±" to "Alt +", or mapping "Shift {keycode for = }" to "+". However, there don't seem to be any guarantees that this would work across different keyboard layouts; and it certainly isn't good coding style.
If anyone can suggest a way around these problems, or perhaps point be to code that's already handled this difficulty, I'd be most appreciative.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
其中
e
是KeyEvent
并且它在keyPressed
方法中处理。当您在指定的键盘上按 ALT+Shift+= 时,上面的代码将打印 Plus。
有关完整的工作代码,请参阅下面的示例:
希望这会有所帮助。
Try this:
Where
e
is theKeyEvent
and it is handled inkeyPressed
method.The above code will print Plus when you press ALT+Shift+= on the keyboard specified by you.
For complete working code see the below example:
Hope this will help.