我尝试从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它就不起作用