[英]change physical keyboard on android programmatically (Android Studio)
I want to change the physical keyboard layout to Hebrew when using my keyboard in a programmatic way. 我想以编程方式使用键盘时将物理键盘布局更改为希伯来语。 Right now when I press on the physical keyboard it inputs English characters. 现在,当我按下物理键盘时,它会输入英文字符。 I'm developing my app for Android lollipop and higher. 我正在为Android棒棒糖及更高版本开发我的应用程序。
OK after some research I found out a solution for this problem - I had to implement the KeyEvent.Callback interface, and override the onKeyDown method. 经过一番研究后,我找到了解决此问题的方法-我必须实现KeyEvent.Callback接口,并重写onKeyDown方法。 This method inputs me the key code that was pressed, and I have to translate it into the character in the language I want to write in. This is what the code looks like: 该方法输入了我按下的键代码,我必须将其转换为我要编写的语言中的字符。代码如下所示:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Toast.makeText(getApplicationContext(),"Keycode="+keyCode,Toast.LENGTH_LONG).show();
handleKey('ש');
return true;
}
public void handleKey(int keyCode)
{
InputConnection inputConnection = getCurrentInputConnection();
if (inputConnection != null) {
switch(keyCode) {
case Keyboard.KEYCODE_DELETE :
CharSequence selectedText = inputConnection.getSelectedText(0);
if (TextUtils.isEmpty(selectedText)) {
inputConnection.deleteSurroundingText(1, 0);
} else {
inputConnection.commitText("", 1);
}
break;
default :
char code = (char) keyCode;
inputConnection.commitText(String.valueOf(code), 1);
}
}
Here a link to where I found this solution on Google's documentation: https://developer.android.com/training/keyboard-input/commands 这是我在Google文档中找到该解决方案的链接: https : //developer.android.com/training/keyboard-input/commands
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.