Code example for KeyEvent

Methods: getActiongetKeyCodegetUnicodeChar

0
        if (DEBUG) Log.w(TAG, "sendKeyEvent [" + event.getAction() + "]");
 
        // If this is a key-up, and backspace/del or if the key has a character representation, 
        // need to update the underlying Editable (i.e. the local representation of the text 
        // being edited). 
        if (event.getAction() == KeyEvent.ACTION_UP) {
            if (event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
                super.deleteSurroundingText(1, 0); 
            } else if (event.getKeyCode() == KeyEvent.KEYCODE_FORWARD_DEL) {
                super.deleteSurroundingText(0, 1); 
            } else { 
                int unicodeChar = event.getUnicodeChar();
                if (unicodeChar != 0) {
                    Editable editable = getEditable();
                    int selectionStart = Selection.getSelectionStart(editable);
                    int selectionEnd = Selection.getSelectionEnd(editable);
                    if (selectionStart > selectionEnd) {
                        int temp = selectionStart;
                        selectionStart = selectionEnd;
                        selectionEnd = temp;
                    } 
                    editable.replace(selectionStart, selectionEnd,
                            Character.toString((char)unicodeChar));
                } 
            } 
        } else if (event.getAction() == KeyEvent.ACTION_DOWN) {
            // TODO(aurimas): remove this workaround when crbug.com/278584 is fixed. 
            if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                beginBatchEdit(); 
                finishComposingText(); 
                mImeAdapter.translateAndSendNativeEvents(event);
                endBatchEdit(); 
                return true;