Code example for KeyEvent

Methods: getActiongetKeyCode

0
    } 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        Log.e( "keycode=","keycode="+keyCode ); 
 
        int keycode = event.getKeyCode();
 
        if ( event.getAction() == KeyEvent.ACTION_DOWN ){
            switch(keycode){
                case KeyEvent.KEYCODE_DPAD_CENTER:
                    return doFunction(mDpadCenterFunction);
            } 
        } 
 
        return super.onKeyDown(keyCode, event);
    } 
    @Override 
    public boolean dispatchKeyEventPreIme(KeyEvent event) {
//        Log.e( "dispatch=","keycode="+event.getKeyCode() ); 
 
        int keycode = event.getKeyCode();
        // ALT + KEYDOWN 
        int meta = (int)event.getMetaState();
//        int altstate = TextKeyListener.getMetaState(cs,KeyEvent.META_ALT_ON); 
        boolean alt = (meta & mShortcutMetaKey)!=0 ; // || (altstate!=0);      // one of meta keies is pressed , or , Alt key is locked
 
        if ( alt && event.getAction() == KeyEvent.ACTION_DOWN ){
            if (doShortcut(keycode)){
//                if ( altstate == 1 ){ 
//                    TextKeyListener.clearMetaKeyState(cs,KeyEvent.META_ALT_ON); 
//                } 
                if ( (meta & KeyEvent.META_ALT_LEFT_ON )!=0 || (meta & KeyEvent.META_ALT_RIGHT_ON )!=0 )
                { 
                    InputMethodManager imm = InputMethodManager.peekInstance();
                    if (imm != null){
                        // for IS01 w/iWnn 
                        // iWnn eats ALT key so we needs to reset ime. 
                        try { 
                            Class<?> c = imm.getClass();
                            Field f = c.getDeclaredField("mCurId");
                            f.setAccessible(true);
                            String immId = (String)f.get(imm);
                            if ( "jp.co.omronsoft.iwnnime/.iWnnIME".equals(immId) ){
                                imm.restartInput(this);
                            } 
                        } catch (Exception e) {
                        } 
                    } 
                    TextKeyListener.resetMetaState((Spannable)getEditableText());
                } 
                return true; 
            } 
 
        } 
        return super.dispatchKeyEventPreIme(event);
    } 
 
    public boolean doFunction( int function ){
        boolean result = doFunction_( function );
        return result;
    } 
 
 
    public boolean doFunction_( int function ){
        ShortcutListener sl = mShortcutListener.get();
 
        switch ( function) {
            case FUNCTION_SELECT_ALL:
                return onKeyShortcut(KeyEvent.KEYCODE_A, new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_A));
 
            case FUNCTION_CUT:
                return onKeyShortcut(KeyEvent.KEYCODE_X, new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_X));
 
            case FUNCTION_COPY:
                return onKeyShortcut(KeyEvent.KEYCODE_C, new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_C));
 
            case FUNCTION_UNDO:
                return onKeyShortcut(KeyEvent.KEYCODE_Z, new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_Z));
 
            case FUNCTION_REDO:
                return onKeyShortcut(KeyEvent.KEYCODE_Y, new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_Y));
 
            case FUNCTION_PASTE:
                return onKeyShortcut(KeyEvent.KEYCODE_V, new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_V));
 
 
            case FUNCTION_SAVE:
                if (sl != null) {
                    return sl.onCommand(KeyEvent.KEYCODE_S);
                } 
                break; 
 
            case FUNCTION_DIRECTINTENT:
                if (sl != null) {
                    return sl.onCommand(KeyEvent.KEYCODE_D);
                } 
                break; 
 
            case FUNCTION_ENTER:
                return onKeyDown(KeyEvent.KEYCODE_ENTER ,new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_ENTER));
            case FUNCTION_TAB:
                return onKeyDown(KeyEvent.KEYCODE_TAB ,new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_TAB));
            case FUNCTION_DEL:
                return onKeyDown(KeyEvent.KEYCODE_DEL ,new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DEL));
            case FUNCTION_FORWARD_DEL: