Code example for InputConnection

Methods: sendKeyEvent

0
     
    /** Helper to send a key down / key up pair to the current editor. */ 
    private void keyDownUp(int keyEventCode){
    	InputConnection ic=this.getCurrentInputConnection();
    	KeyEvent e=new KeyEvent(ACTION_DOWN, keyEventCode);
        ic.sendKeyEvent(e);
        e=new KeyEvent(ACTION_UP, keyEventCode);
        ic.sendKeyEvent(e);
    } 
     
    /** Helper to send a character to the editor as raw key events. */ 
    private void sendKey(int keyCode){
        switch (keyCode) {
            case '\n': 
                this.keyDownUp(KeyEvent.KEYCODE_ENTER);
                break; 
            default: 
                if (keyCode >= '0' && keyCode <= '9')
                    keyDownUp(keyCode - '0' + KeyEvent.KEYCODE_0);
                else 
                    getCurrentInputConnection().commitText(String.valueOf((char) keyCode), 1);