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. I'm saving the keyStroke.getkeyCode()
and keyStroke.getModifiers()
to a file to re-create the shortcuts when the application is restarted. As I understand this, these values directly correspond to the equivalent methods of the KeyEvent
class which are based on the VK_
constants.
The KeyEvent
class has the following warning:
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. Sun reserves the right to change these values as needed to accomodate a wider range of keyboards in the future.
So does that mean I shouldn't store a keyCode
between sessions, because it may change in future version of 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.
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.