如何使用 InputMethodService 更改 usb 键盘布局语言?

问题描述 投票:0回答:0

我在 kitkat android 上有一台智能电视,我想连接一个 usb 键盘并像在 Windows 中一样设置键盘快捷键,以免因不方便(标准)的键盘快捷键而感到不适,我也想添加自己的Google Play 中可用的应用程序中不可用的功能的实现。

我尝试通过以下程序设置键盘快捷键: 1.外接键盘助手专业版 在这个应用程序中,语言切换是我需要的,但应用程序不知道多少,我设法实现了所有其他功能,但没有语言切换。 2.Button Mapper: Remap your keys 该应用程序需要启用无障碍功能,但智能电视固件被削减,无法启用无障碍功能。

因此,我正在写我的申请,因为有向这个方向前进的需要和愿望。

决定制作自己的应用程序但面临切换键盘布局的问题

public class ServiceIME1
extends InputMethodService{
private static final String TAG = ServiceIME.class.getSimpleName();
private static int pair[] = new int[2];
    
@Override
public boolean onKeyDown(int keyCode, KeyEvent keyEvent){
    switch (keyCode){
    case KeyEvent.KEYCODE_ALT_LEFT:
    case KeyEvent.KEYCODE_CTRL_LEFT:
    case KeyEvent.KEYCODE_META_LEFT:
                pair[0] = keyCode;
                return true;
                
    case KeyEvent.KEYCODE_SHIFT_LEFT:
                if (pair[0] == KeyEvent.KEYCODE_CTRL_LEFT){         //"ctrl+shift"          InputMethodManager inputMethodManager = (InputMethodManager) getApplicationContext().getSystemService(getApplicationContext().INPUT_METHOD_SERVICE);
                    inputMethodManager.showInputMethodPicker();         }
                if (pair[0] == KeyEvent.KEYCODE_ALT_LEFT){          // alt+shift            // switch lang
                    Locale locale1 = new Locale("en", "US");
                    Locale locale2 = new Locale("ru", "RU");

                    Locale l1 = Locale.getDefault();

                    Log.d(TAG, "" + l1.toString().equals(locale2.toString()));

                    if (l1.toString().equals(locale2.toString())){
                        l1.setDefault(locale1);
                    }
                    if (l1.toString().equals(locale1.toString())){
                        l1.setDefault(locale2);
                    }
                    
                    Toast.makeText(getApplicationContext(), l1.getDisplayLanguage(), Toast.LENGTH_SHORT).show();
                    
                    return true;
                }
                return true;
                
            default:
return super.onKeyDown(keyCode, keyEvent);
}
}

@Override 
public boolean onKeyUp(int keyCode, KeyEvent event){
    switch (keyCode)
    {
case KeyEvent.KEYCODE_ALT_LEFT:
case KeyEvent.KEYCODE_CTRL_LEFT:
case KeyEvent.KEYCODE_META_LEFT:
                pair[0] = 0;
                return true;

            default:
                return super.onKeyUp(keyCode, event);
}
}
}

尝试了在这个论坛上找到的解决方案:

//1
                try{
                    String keyCommand = "input keyevent " + KeyEvent.KEYCODE_LANGUAGE_SWITCH;
                    Runtime runtime = Runtime.getRuntime();
                    Process proc = runtime.exec(keyCommand);
                }
                catch (IOException e){
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

//2

new KeyEvent(KeyEvent.ACTION_DOWN, 
KeyEvent.KEYCODE_LANGUAGE_SWITCH);

//3
                InputConnection inputConnection = this.getCurrentInputConnection();
                if (inputConnection != null) {
                    inputConnection.sendKeyEvent(new KeyEvent(
                    keyEvent.getDownTime(), 
                    keyEvent.getEventTime(), 
                    keyEvent.getAction(), 
                    KeyEvent.KEYCODE_LANGUAGE_SWITCH,               keyEvent.getRepeatCount(),              0,              keyEvent.getDeviceId(),             keyEvent.getScanCode(),         keyEvent.getFlags()
                    ));}

//4
                Locale locale = new Locale("ru");
                Locale.setDefault(locale);
                Configuration config = getBaseContext().getResources().getConfiguration();
                config.locale = locale;
                getBaseContext().getResources().updateConfiguration(
                config,getBaseContext().getResources().getDisplayMetrics());

//5
Locale locale1 = new Locale("en", "US");
Locale locale2 = new Locale("ru", "RU");

Locale l1 = Locale.getDefault();

if (l1.toString().equals(locale2.toString())){
        l1.setDefault(locale1);
Resources resources = getResources();
Configuration configuration = resources.getConfiguration();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){   configuration.setLocale(locale1);
}else{
            configuration.locale = locale1;}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N){
            getApplicationContext().createConfigurationContext(configuration);}else{    resources.updateConfiguration(configuration, displayMetrics);}
}
if (l1.toString().equals(locale1.toString())){
        l1.setDefault(locale2);
Resources resources = getResources();
Configuration configuration = resources.getConfiguration();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
            configuration.setLocale(locale2);}else{

configuration.locale = locale2;}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N){
getApplicationContext().createConfigurationContext(configuration);}else{
    resources.updateConfiguration(configuration, displayMetrics);
}
}

Log.d(TAG, l1.getDisplayLanguage().toString());
Log.d(TAG, l1.toString());
    Toast.makeText(getApplicationContext(), "current lang= " + l1.getDisplayLanguage().toString(), Toast.LENGTH_SHORT).show();

希望得到您的帮助。

android keyboard key-bindings custom-keyboard input-method
© www.soinside.com 2019 - 2024. All rights reserved.