Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 1468 times)
0 Members and 1 Guest are viewing this topic.
Offline Jacob_

Junior Member


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 Knight


Medals: 12


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).

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline zoto

Senior Member


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 Member


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 Ninja


Medals: 49



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

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

1  
2  
        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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (19 views)
2014-09-12 09:08:26

Norakomi (54 views)
2014-09-10 13:57:51

TehJavaDev (63 views)
2014-09-10 06:39:09

Tekkerue (31 views)
2014-09-09 02:24:56

mitcheeb (53 views)
2014-09-08 06:06:29

BurntPizza (37 views)
2014-09-07 01:13:42

Longarmx (23 views)
2014-09-07 01:12:14

Longarmx (27 views)
2014-09-07 01:11:22

Longarmx (27 views)
2014-09-07 01:10:19

mitcheeb (35 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!