`
大器晚成
  • 浏览: 51509 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

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

        }
    }
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics