Вы можете захватывать события вашей Softkeyboard и передавать их в ваши собственные виджеты, применяя KeyboardView.OnKeyboardActionListener
.
В вашем Inputmethodservice.onKey()
методе, вы должны попытаться передать событие на вашПодвиды InputView, такие как:
public class mySoftKeyboard
extends InputMethodService
implements KeyboardView.OnKeyboardActionListener {
// Implementation of KeyboardViewListener inside your InputMethodService
public void onKey(int primaryCode, int[] keyCodes) {
//assuming your inputview is in private variable mInputView
//and contains public members txtTst and edtTst views
//(arrange for this in your InputView.onCreate)
//Here, we just transmit the onKey code to View.onKeyDown/Up and let views draw themselves
sendKey( mInputView.txtTst , primaryCode ); // send this to your TextView
sendKey( mInputView.edtTst , primaryCode ); // also send to your EditText
}
/**
* Helper to send a character to the editor as raw key events.
*/
private void sendKey(View v, int keyCode) {
v.onKeyDown(keyCode,new KeyEvent(KeyEvent.ACTION_DOWN, keyCode));
v.onKeyUp (keyCode,new KeyEvent(KeyEvent.ACTION_UP, keyCode));
}
//other interface function, no need to implement
public void onText(CharSequence text){}
public void swipeRight() {}
public void swipeLeft() {}
public void swipeDown() {}
public void swipeUp() {}
public void onPress(int primaryCode) {}
public void onRelease(int primaryCode) {}
}
Edit
Чтобы ответить на ваш комментарий о разнице между глифом и кодом ключа, вот фрагмент кода, который может помочь вам:
//This snippet tries to translate the glyph 'primaryCode' into key events
//retrieve the keycharacter map from a keyEvent (build yourself a default event if needed)
KeyCharacterMap myMap=KeyCharacterMap.load(event.getDeviceId());
//event list to reproduce glyph
KeyEvent evs[]=null;
//put the primariCode into an array
char chars[]=new char[1];
chars[0]=primaryCode;
// retrieve the key events that could have produced this glyph
evs=myMap.getEvents(chars);
if (evs != null){
// we can reproduce this glyph with this key event array
for (int i=0; i< evs.length;i++) mySendKeyMethodHelper(evs[i]);
}
else { /* could not find a way to reproduce this glyph */ }