1. 程式人生 > >JMonkeyEngine jme裡JMEDesktop輸入中文解決方案

JMonkeyEngine jme裡JMEDesktop輸入中文解決方案

修改該類原始碼,主要是這兩個方法。測試環境簡體中文XP3測試通過
  
// Properties props = System.getProperties();
// String charset=props.getProperty("sun.jnu.encoding");
// "GBK"是輸入時的環境,一般中文輸入法都採用該字符集吧
char chcode=0;
public void onKey( final char character, final int keyCode, final boolean pressed ) {
System.out.println("--"+(int)character);
if (chcode==0) {
chcode=character;
if (character>127) {
return;
}
}else {
int temp=chcode;
chcode = (char) (character + (chcode << 8));
System.out.println(((int)temp<< 8)+"+"+(int)(character)+"="+((int)chcode));

}
try {
SwingUtilities.invokeAndWait( new Runnable() {
public void run() {
sendAWTKeyEvent( keyCode, pressed, chcode);
chcode=0;
}
} );
} catch ( InterruptedException e ) {
logger.logp(Level.SEVERE, this.getClass().toString(),
"onKey(character, keyCode, pressed)", "Exception", e);
} catch ( InvocationTargetException e ) {
logger.logp(Level.SEVERE, this.getClass().toString(),
"onKey(character, keyCode, pressed)", "Exception", e);
}
}

private void sendAWTKeyEvent( int keyCode, boolean pressed, char character ) {
keyCode = AWTKeyInput.toAWTCode( keyCode );
try {
if ( keyCode != 0 ) {
Component focusOwner = getFocusOwner();
if ( focusOwner == null ) {
focusOwner = desktop;
}
if ( character == '\0' ) {
character = KeyEvent.CHAR_UNDEFINED;
}
if ( focusOwner != null ) {
System.out.println("sendAWTKeyEvent:"+(int)character);
if ( pressed ) {
KeyEvent event = new KeyEvent( focusOwner, KeyEvent.KEY_PRESSED,
System.currentTimeMillis(), getCurrentModifiers( -1 ),
keyCode, character );
dispatchEvent( focusOwner, event );
System.out.println("dispatchEvent1:"+(int)character);
anInt.value = keyCode;
Char c = characters.get( anInt );
if ( c == null ) {
characters.put( new Int( keyCode ), new Char( character ) );
}
else {
c.value = character;
}
if ( character != KeyEvent.CHAR_UNDEFINED ) {
dispatchEvent( focusOwner, new KeyEvent( focusOwner, KeyEvent.KEY_TYPED,
System.currentTimeMillis(), getCurrentModifiers( -1 ),
0, character ) );
System.out.println("dispatchEvent2:"+(int)character);
}
}
if ( !pressed ) {
anInt.value = keyCode;
// Char c = characters.get( anInt );
// if ( c != null ) {
// character = c.value;
// //TODO: repeat input
//// if ( character != KeyEvent.CHAR_UNDEFINED ) {
//// dispatchEvent( focusOwner, new KeyEvent( focusOwner, KeyEvent.KEY_TYPED,
//// System.currentTimeMillis(), getCurrentModifiers( -1 ),
//// 0, character ) );
//// }
// }
System.out.println(focusOwner instanceof JTextField);
System.out.println((int)character+","+(character>255&&character<65535));
if (character>255&&character<65535) {
if (focusOwner instanceof JTextField) {
((JTextField)focusOwner).getDocument().insertString(((JTextField)focusOwner).getCaretPosition(), new String(new byte[]{(byte)(character/256),(byte)(character%256)},"gbk"), null);//getDocument().getStartPosition().insertString(zOrder, name, null);
System.out.println("---"+new String(new byte[]{(byte)(character/256),(byte)(character%256)},"gbk").toCharArray()[0]);
}
}else {
dispatchEvent( focusOwner, new KeyEvent( focusOwner, KeyEvent.KEY_RELEASED,
System.currentTimeMillis(), getCurrentModifiers( -1 ),
keyCode, character ) );
}
}
}
}
} catch (Exception e) {

}
}