Всем мир!
Не получается реализовать переключение раскладки usb клавиатуры подключенной к андройд устройству.
Приложения в которых реализована возможность переключения раскладки клавиатуры:
1.External Keyboard Helper Pro - В этом приложении нужная реализация имеется, реализовано через InputMethodService, но не понятно как он это реализовал.
2.Russian Keyboard тоже имеется возможность изменения раскладки без использования специальных возможностей, тот же InputMethodService.
3.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();
//6
private IBinder getToken() {
final Dialog dialog = getWindow();
if (dialog == null) {
return null;
}
final Window window = dialog.getWindow();
if (window == null) {
return null;
}
return window.getAttributes().token;
}
private void handleLanguageSwitch() {
mInputMethodManager.switchToNextInputMethod(getToken(), false /* onlyCurrentIme */);
}
Заранее спасибо:)