简体   繁体   中英

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.

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM