从EditText发送KeyEvent

时间:2014-08-13 06:32:48

标签: android

我尝试从EditText发送 keyevent 。为此,我使用了 InputConnectionWrapper ,这是我从方法onCreateInputConnection()中获取的:

ZanyInputConnection zic;
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    zic = new ZanyInputConnection(super.onCreateInputConnection(outAttrs),
            true);
    return zic;
}

ZanyInputConnection里面,我写了一个开启班次的方法:

private class ZanyInputConnection extends InputConnectionWrapper {

    public ZanyInputConnection(InputConnection target, boolean mutable) {
        super(target, mutable);
    }

    public void turnOnShift(){
        long eventTime = SystemClock.uptimeMillis();
        sendKeyEvent(new KeyEvent(eventTime, eventTime,
                KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
                KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE));
         sendKeyEvent(new KeyEvent(eventTime, SystemClock.uptimeMillis(),
                KeyEvent.ACTION_UP, KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
                KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE));

    }
} 

它在onSelectionChange()方法内部调用:

@Override
public void onSelectionChanged(int selStart, int selEnd) {
    zic.turnOnShoft();
}

但是当方法被称为无事件时。

修改

如果我稍后使用KeyEvent.KEYCODE_A'a'添加,但是如果我使用KeyEvent.KEYCODE_SHIFT_LEFT它就不起作用

0 个答案:

没有答案