[英]Best way to let the user choose a hotkey and then save it?
I get a KeyStroke
object from a KeyEvent
(from a keyPressed
event with a KeyListener
attached to a JTextField
) that I use to create local and global shortcuts. 我从用来创建本地和全局快捷方式的
KeyEvent
(来自具有附加到JTextField
的KeyListener
的keyPressed
事件)中获取KeyStroke
对象。 I'm saving the keyStroke.getkeyCode()
and keyStroke.getModifiers()
to a file to re-create the shortcuts when the application is restarted. 我将
keyStroke.getkeyCode()
和keyStroke.getModifiers()
到文件中,以便在重新启动应用程序时重新创建快捷方式。 As I understand this, these values directly correspond to the equivalent methods of the KeyEvent
class which are based on the VK_
constants. 据我了解,这些值直接对应于基于
VK_
常量的KeyEvent
类的等效方法。
The KeyEvent
class has the following warning: KeyEvent
类具有以下警告:
WARNING: Aside from those keys that are defined by the Java language (VK_ENTER, VK_BACK_SPACE, and VK_TAB), do not rely on the values of the VK_ constants.
警告:除了Java语言(VK_ENTER,VK_BACK_SPACE和VK_TAB)定义的那些键外,不要依赖VK_常量的值。 Sun reserves the right to change these values as needed to accomodate a wider range of keyboards in the future.
Sun保留根据需要更改这些值的权利,以在将来适应更大范围的键盘。
So does that mean I shouldn't store a keyCode
between sessions, because it may change in future version of Java? 那么这是否意味着我不应该在会话之间存储
keyCode
,因为它可能会在Java的未来版本中更改? If so, what should I store instead? 如果是这样,我应该存储什么呢? I was thinking of storing the name of the key as it is used in the
VK_
constants and also used for one of the KeyStroke.getKeyStroke()
methods, but I'm not sure whether that would be better and how to actually get the appropriate String. 我正在考虑存储键的名称,因为它在
VK_
常量中使用,并且也用于KeyStroke.getKeyStroke()
方法之一,但是我不确定这是否会更好,以及如何实际获得合适的键串。
Also I'm not quite sure how to best handle different keyboard layouts when capturing the hotkey. 另外,我不太确定在捕获热键时如何最好地处理不同的键盘布局。 It seems like
getKeyCode()
sometimes yields an undefined code on different layouts. 似乎
getKeyCode()
有时会在不同布局上产生未定义的代码。
It's unlikely that the values will change, but yes, it is possible they might. 这些值不太可能会改变,但是是的,它们可能会改变。
The easiest solution is to store the string returned by calling the KeyStroke's toString() method. 最简单的解决方案是存储通过调用KeyStroke的toString()方法返回的字符串。 That string is guaranteed to be parseable by KeyStroke.getKeyStroke(String) , which is what you should call when reading the saved preference.
该字符串保证可以通过KeyStroke.getKeyStroke(String)进行解析,这是读取保存的首选项时应调用的名称。
I think your problem with undefined key codes can be solved if you use getExtendedKeyCode() rather than getKeyCode(). 我认为,如果使用getExtendedKeyCode()而不是getKeyCode(),则可以解决未定义键代码的问题。
While it's likely that new key codes would represent a superset of existing codes, the user may change or add keyboards at any time. 尽管新的键代码可能代表现有代码的超集,但用户可以随时更改或添加键盘。 This risk may be mitigated by allowing the user to reassign keys needed for a novel keyboard layout .
通过允许用户重新分配新颖的键盘布局所需的键,可以减轻这种风险。 In this complete example,
org.gcs.robot.RobotChase
manages an enum Key
of all keystrokes known to the application. 在这个完整的示例中,
org.gcs.robot.RobotChase
管理该应用程序已知的所有击键的enum Key
。 RCKeys
, seen here , displays a dialog that allows the user to reassign keys arbitrarily. 在此看到的
RCKeys
显示了一个对话框,允许用户随意重新分配键。 RCPrefs
includes methods to save and restore the chosen key code values in an instance of java.util.prefs.Preferences
. RCPrefs
包括在java.util.prefs.Preferences
实例中保存和恢复所选键代码值的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.