https://developer.android.google.cn
onKey
added in
API level 3
Send a key press to the listener.
| Parameters | |
|---|---|
primaryCode | int: this is the key that was pressed |
keyCodes | int: the codes for all the possible alternative keys with the primary code being the first. If the primary key code is a single character such as an alphabet or number or symbol, the alternatives will include other characters that may be on the same key or adjacent keys. These codes are useful to correct for accidental presses of a key adjacent to the intended key. |
官方解释够权威了吧。在 softkeyboard 示例代码里,keyCodes 的使用出现 3 次。
public void onKey(int primaryCode, int[] keyCodes) {
if (isWordSeparator(primaryCode)) {
// Handle separator
if (mComposing.length() > 0) {
commitTyped(getCurrentInputConnection());
}
sendKey(primaryCode);
updateShiftKeyState(getCurrentInputEditorInfo());
} else if (primaryCode == Keyboard.KEYCODE_DELETE) {
handleBackspace();
} else if (primaryCode == Keyboard.KEYCODE_SHIFT) {
handleShift();
} else if (primaryCode == Keyboard.KEYCODE_CANCEL) {
handleClose();
return;
} else if (primaryCode == LatinKeyboardView.KEYCODE_OPTIONS) {
// Show a menu or somethin'
} else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE
&& mInputView != null) {
Keyboard current = mInputView.getKeyboard();
if (current == mSymbolsKeyboard || current == mSymbolsShiftedKeyboard) {
current = mQwertyKeyboard;
} else {
current = mSymbolsKeyboard;
}
mInputView.setKeyboard(current);
if (current == mSymbolsKeyboard) {
current.setShifted(false);
}
} else {
handleCharacter(primaryCode, keyCodes);
}
}
private void handleCharacter(int primaryCode, int[] keyCodes) {
if (isInputViewShown()) {
if (mInputView.isShifted()) {
primaryCode = Character.toUpperCase(primaryCode);
}
}
if (isAlphabet(primaryCode) && mPredictionOn) {
mComposing.append((char) primaryCode);
getCurrentInputConnection().setComposingText(mComposing, 1);
updateShiftKeyState(getCurrentInputEditorInfo());
updateCandidates();
} else {
getCurrentInputConnection().commitText(
String.valueOf((char) primaryCode), 1);
}
}
使用多键的例子:
<Key android:codes="46,44" android:keyLabel=". ,"
android:keyWidth="15%p"/>
(1)在 softkeyboard 示例里,keyCodes 多键值是有效的;
(2)在 strocomp 代码里,初始化 android:codes = "0,0",获取 keyCodes[1] 为 -1。不知为何?
存疑。
本文详细解析了Android软键盘中onKey方法的工作原理及其参数作用,特别是primaryCode和keyCodes的使用方式。通过示例代码展示了如何处理不同类型的按键输入,并讨论了多键值的有效性和实现方法。
2006

被折叠的 条评论
为什么被折叠?



