I have a smart TV on android, I want to connect a usb keyboard and set up keyboard shortcuts like in Windows, so as not to feel discomfort from non-convenient (standard) keyboard shortcuts, and I also want to add my own implementations of functionality that are not available in applications available in Google Play.
I tried to set keyboard shortcuts through the following programs: 1.External Keyboard Helper Pro In this application, language switching is done as I need, but the application does not know how much, I managed to implement all the other functions, but there is no language switching. 2.Button Mapper: Remap your keys the application requires accessibility to be enabled, but smart TV firmware is cut down and there is no way to enable accessibility.
Therefore, I am writing my application, as there is a need and a desire to move in this direction.
Decided to make my own app but faced the problem of switching keyboard layout
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);
}
}
}
Tried the solutions found on this forum:
//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();