Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Converting AWT key codes to characters  (Read 2333 times)
0 Members and 1 Guest are viewing this topic.
Offline Jacob_

Junior Devvie

Projects: 3

« Posted 2010-10-19 00:44:35 »

Is there a way to convert a key code into the character that goes with it? It needs to be like KeyEvent.getKeyChar(), but not require a KeyEvent object.
Offline Abuse

JGO Ninja

Medals: 64

falling into the abyss of reality

« Reply #1 - Posted 2010-10-19 02:29:02 »

If you just want the keycode name (for example "VK_A") associated with a given keycode value, you can use reflection on the KeyEvent class to retrieve this information.

If on the other hand you want to convert a keycode into a character, then you will soon realize this is impossible as it is not a one-to-one relationship.

Keycodes relate to the physical keys on the keyboard - as such some generate no character (e.g. ALT), while others generate many different characters (depending upon the state of the shift/alt/control keys & num/func/caps lock keys).
Offline zoto

Senior Devvie

Medals: 4

« Reply #2 - Posted 2010-10-19 02:31:31 »

What about KeyEvent.getKeyText(int keyCode)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jacob_

Junior Devvie

Projects: 3

« Reply #3 - Posted 2010-10-19 20:05:36 »

What about KeyEvent.getKeyText(int keyCode)

But for symbols that returns stuff like "Equals" and "Left Bracket"; I need the actual character.

I'm not actually going to display the character, so modifier keys don't matter. The game has Lua scripting so I want to give the user something more friendly than the code to work with.

I suppose I could just make an empty hashtable of key codes/characters and add values to it in my key event handler.
Offline cylab

JGO Kernel

Medals: 180

« Reply #4 - Posted 2010-10-20 08:46:04 »

Take a look at the source of KeyEvent (Codesearch) line 549:

        this(src, id, when, modifiers, keyCode,
               (keyCode > (2 << 7) - 1) ? CHAR_UNDEFINED : (char) keyCode);

looks like the keyCode simply is the ASCI char for codes < 127 (which is the result of  (2 << 7) - 1) - seems like someone had his funny day when writing this code...), so just cast it to (char) and you are done.

Mathias - I Know What [you] Did Last Summer!
Pages: [1]
  ignore  |  Print  

EgonOlsen (45 views)
2018-06-10 19:43:48

EgonOlsen (27 views)
2018-06-10 19:43:44

EgonOlsen (47 views)
2018-06-10 19:43:20

DesertCoockie (210 views)
2018-05-13 18:23:11

nelsongames (127 views)
2018-04-24 18:15:36

nelsongames (126 views)
2018-04-24 18:14:32

ivj94 (867 views)
2018-03-24 14:47:39

ivj94 (128 views)
2018-03-24 14:46:31

ivj94 (779 views)
2018-03-24 14:43:53

Solater (143 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!