简体   繁体   English

让用户选择一个热键然后保存的最佳方法?

[英]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 (来自具有附加到JTextFieldKeyListenerkeyPressed事件)中获取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.

相关问题 让用户选择应用程序以打开文件 - Let user choose the application to open the file 通过将任务实体与其所属的用户相关联来保存任务实体的最佳方法是什么? - What is the best way to save a task entity by associating it with the user it belongs to? 在java应用程序中保存用户设置的最佳方法是什么? - What is the best way to save user settings in java application? 保存数组列表的最佳方法 - Best way to save a arraylist 让用户在 Web 应用程序中保存文件时选择文件类型 - Let user choose file type while saving a file in Web application 如何允许用户选择保存位置和文件名 - How to allow the user to choose save location and filename 验证用户的最佳方法 - Best way to authenticate user 将SXSSFWorkbook保存到blob的最佳方法? - Best way to save SXSSFWorkbook to blob? 保存在Java桌面应用程序应用程序和网站上使用的个人用户设置的最佳方法 - Best way to save personal user settings who are used on java desktop application app and website 为ConcurrentHashMap选择并发级别的最佳方法是什么? - What's the best way to choose the concurrency level for a ConcurrentHashMap?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM