Last active
January 28, 2016 19:58
-
-
Save xkr47/3d35584496859a599b72 to your computer and use it in GitHub Desktop.
How to get the private rawCode field out of a Java Swing KeyEvent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.event { | |
KeyEvent | |
} | |
import java.lang { | |
Runnable, | |
JBoolean=Boolean, | |
JLong=Long | |
} | |
import java.lang.reflect { | |
Field | |
} | |
Field getRawCodeField() { | |
value c = javaClass<KeyEvent>(); | |
value f = c.getDeclaredField("rawCode"); | |
javaClass<Field>().getMethod("setAccessible", JBoolean.\iTYPE).invoke(f, JBoolean(true)); | |
return f; | |
} | |
Field rawCodeField = getRawCodeField(); | |
shared Integer getRawCode(KeyEvent e) { | |
assert(is JLong v = rawCodeField.get(e)); | |
return v.longValue(); | |
} | |
// ... | |
shared actual void keyPressed(KeyEvent? keyEvent) { | |
if (!exists keyEvent) { return; } | |
value rawCode = getRawCode(keyEvent); | |
print(rawCode); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And if you want to get events for the TAB key as well, remember to do