Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
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  
  How to convert KeyCode to text.  (Read 9334 times)
0 Members and 1 Guest are viewing this topic.
Offline zammbi

JGO Coder


Medals: 4



« Posted 2009-06-11 19:36:53 »

I'm making a key control options. I save my keys as KeyCode's but how to I convert this to string?
toString() gives me "VK_A" for A, which I only want 'A'.

Current project - Rename and Sort
Offline PeterB

Junior Member





« Reply #1 - Posted 2009-06-11 20:30:48 »

You should have access to the KeyEvent object, from which you can get either the key code (int) or the key char (char).

From the javadocs:

char  getKeyChar()
          Returns the character associated with the key in this event.

 int    getKeyCode()
          Returns the integer keyCode associated with the key in this event.

Vault101 / Mace The Game
There are 10 kinds of people in the world. Those who understand binary and those who don't.
Offline zoto

Senior Member


Medals: 4



« Reply #2 - Posted 2009-06-12 03:11:05 »

KeyEvent.getKeyText(int keyCode);
KeyEvent.getKeyModifiersText(int modifiers);
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zammbi

JGO Coder


Medals: 4



« Reply #3 - Posted 2009-06-12 07:07:49 »

Those methods don't seem to be in JavaFX. I don't want to use Java otherwise it won't be portable.

Current project - Rename and Sort
Offline trembovetski

Senior Member




If only I knew what I'm talking about!


« Reply #4 - Posted 2009-06-12 21:15:02 »

Those methods don't seem to be in JavaFX. I don't want to use Java otherwise it won't be portable.


http://java.sun.com/javafx/1.2/docs/api/javafx.scene.input/javafx.scene.input.KeyEvent.html

Note the 'text' field.
Offline zammbi

JGO Coder


Medals: 4



« Reply #5 - Posted 2009-06-13 06:17:48 »

Yes I know that, but I'm working with KeyCode's not KeyEvent's.
Is it possible to convert a Keycode back into a KeyEvent?

What I'm doing is making custom controls and on load up I create my own default KeyCodes and then if the user wants too, they can over write them to there liking.

Current project - Rename and Sort
Offline aNt

Senior Member




AFK


« Reply #6 - Posted 2009-06-13 09:07:32 »


this is how we did it in darkchat (maybe a better way).

first do a onKeyPressed on your node:

1  
2  
3  
onKeyPressed: function(e:KeyEvent):Void {
      handleKeyInput(e.code.toString().trim());
}


then do a onKeyTyped on the same node:

1  
2  
3  
onKeyTyped: function(e:KeyEvent):Void {
      handleKeyInput(e.char.toString().trim());
}


then call a function to work out if it was a code or a char:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
function handleKeyInput( keyInput ) {
       if ( keyInput.length() == 1 ) {
             // it is a char
            ....
       } else {
             // it is a keycode
            ....
             if ( keyInput == "VK_BACK_SPACE") {
                    ....
             }
       }
}


bit hackie imo, but what can you do?
Offline zammbi

JGO Coder


Medals: 4



« Reply #7 - Posted 2009-06-13 20:11:25 »

Might just toString() the KeyCode and remove the "VK_" at the front and convert to lower case and swap the underscore for spaces. That would make it fine, and if it doesn't well too bad! heh

There should be a KeyEvent.getKeyText(int keyCode) in JavaFX. Would make things a lot easier.

Edit: Heres the code.

1  
2  
3  
4  
5  
6  
7  
8  
9  
public function KeyCodeString(k:KeyCode):String{
    var s:String = k.toString();
    if(s.startsWith("VK_")){
        s = s.substring(3, s.length());
    }
    s = s.replace("_", " ");

    return s;
}


Current project - Rename and Sort
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.

xsi3rr4x (30 views)
2014-04-15 18:08:23

BurntPizza (27 views)
2014-04-15 03:46:01

UprightPath (42 views)
2014-04-14 17:39:50

UprightPath (24 views)
2014-04-14 17:35:47

Porlus (41 views)
2014-04-14 15:48:38

tom_mai78101 (63 views)
2014-04-10 04:04:31

BurntPizza (123 views)
2014-04-08 23:06:04

tom_mai78101 (222 views)
2014-04-05 13:34:39

trollwarrior1 (189 views)
2014-04-04 12:06:45

CJLetsGame (198 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!