Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Are the KeyEvent VK_* constants stable?!

807580Aug 18 2009 — edited Aug 19 2009
I've been looking everywhere for an answer to this. I'm newish to Java and I've found myself totally flummoxed by something in the documentation that I don't see how can physically make sense, and if it did it wouldn't be sensible design. I'd really appreciate insight from more knowledgeable folks.

My gripe is with the [java.awt.event.KeyEvent|http://java.sun.com/javase/6/docs/api/java/awt/event/KeyEvent.html] api. It defines lots of virtual key code constants for use with the getKeyCode() function. What I need is to have user-configurable key controls for a game, and store them in a config file, so the VK constants seemed perfect.

To my total horror, I saw this note in the documentation:
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.
This is totally useless for any sort of permanent key binding, which is needed to store keys into a file and have them not break in different/future VMs. So I made THIS monstrosity to define better constants: [http://pastebin.com/f73084f4f]

Once I'd updated the rest of my code to use that, it worked perfectly. Then, just to check something, I decided to open up the compiled .class file in Notepad. But the VK_* identifiers are nowhere to be found. They've apparently been inlined by the compiler. I got a disassembler and checked again, and yes, the enums are being initialized like this (with fricking ASCII codes, of all the unrelated things to number virtual key constants with):
A = new StableKey("A", 11, 65);
B = new StableKey("B", 12, 66);
C = new StableKey("C", 13, 67);
D = new StableKey("D", 14, 68);
E = new StableKey("E", 15, 69);
..etcetera..
So what the heck is going on? It says the constants can change, but surely this will break all compiled code even if it isn't storing the key codes externally? Several books have the same ominous caution as the docs, but they all seem to be based on that original note. Is the note wrong? (Its spelling is -- it should be "accommodate".) Or is the VM able to update constants in a way I'm not grasping?

I'm using the ordinary javac that came with the Sun JDK. I've not run the code through any external optimizer.

Please help. This is important to my project. If I release it with the extra constants, I'll never be able to remove them later if they turn out to be completely unnecessary without breaking existing config files, and if I release it without them it might just randomly break anyway later.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 16 2009
Added on Aug 18 2009
11 comments
474 views