Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (468)
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  
  Unicode input  (Read 925 times)
0 Members and 1 Guest are viewing this topic.
Offline fellowusertoo

Junior Newbie





« Posted 2011-07-30 03:38:19 »

jME3 uses LWJGL as a backend. To further explain, in pure LWJGL code, there's this following problem...

1  
2  
3  
4  
5  
char c = Keyboard.getEventCharacter();
System.out.println((int)c); // if this is a unicode character, it will keep printing 63 (which means ?)

char c = (char)29233; // Chinese letter "爱"
System.out.println((int)c); // in this case, it prints 29233 (which is correct).


Apparently, this problem duplicates itself in jME3. Even if you use RawInputListener, to get raw events.

So, I research a little bit. I've found out that this is a bug in java. Microsoft's input method would just return ? for unicode characters. It even happens for TWL. Only pure Java... AWT or Swing input methods will work consistently for unicode input. The only solution is to use AWT or Swing with LWJGL.

Back to jME3, it seems I can't receive events from an attached KeyListener to a canvas to receive its input.

1  
2  
3  
4  
5  
6  
7  
class MyApp extends Application implements KeyListener
...
JmeCanvasContext ctx = (JmeCanvasContext) getContext();
Canvas canvas = ctx.getCanvas();
canvas.addKeyListener(this);
...
// keyPressed, keyReleased and keyTyped events aren't called-back.


Then, I decided to have JTextFields overlay the OpenGL canvas to provide text input, but I found out that it creates more problems. Thus, I abandoned it, in favour of a text field directly in OpenGL instead. I really need unicode textual input as my target application definitely requires handling of East Asian scripts (especially Chinese).

How do unicode games (especially those with chatting consoles) get Unicode Input?
- How do you receive a KeyListener event from an OpenGL canvas?
- Is there a better way to do this?

Thanks for the help! =D
Offline fellowusertoo

Junior Newbie





« Reply #1 - Posted 2011-07-30 13:06:24 »

SOLVED! I definitely needed to use KeyListener or AwtKeyInput, and not LwjglKeyInput in jME3, because LwjglKeyInput did not receive unicode characters properly. So it was a matter of why did KeyListener not receive input.

I found AwtKeyInput in jME3 sources, but it also did not receive input because window and component focus was not set properly. I realised that since it works the same way as using KeyListener, why not, just use KeyListener with LwjglKeyInput.

Therefore, I have a boolean that tells me if my application is in text mode, thus, received KeyListener events instead of LWJGL events. I added a FocusListener. So, if my application is in text mode, the focus is immediately set to the KeyListener implementation class. Otherwise, I cleared focus.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
JmeCanvasContext ctx = (JmeCanvasContext) context;
ctx.setSystemListener(this);

canvas = ctx.getCanvas();
canvas.addFocusListener(this);

frame.addKeyListener(this);
frame.addFocusListener(this);

void focusGained() {
   if(textMode) {
      frame.requestFocus();
      frame.requestFocusInWindow();
   }
   else {
      KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
   }
}

void focusLost() {
   if(textMode) {
      frame.requestFocus();
      frame.requestFocusInWindow();
   }
   else {
      KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
   }
}

Offline Mads

JGO Ninja


Medals: 24
Projects: 3


One for all!


« Reply #2 - Posted 2011-07-30 23:47:04 »

Maybe casting was not a good idea.. Tongue What about the methods char provide, such as getNumericValue()?

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.

theagentd (6 views)
2014-04-24 23:00:44

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

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

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

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

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

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

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

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

trollwarrior1 (217 views)
2014-04-04 12:06:45
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!