Fixed Ctrl key not detected by default system keyboard input method.

If user is using default system keyboard input method, the committext() is called,
otherwise, the sendKeyEvent() is called instead. So it needs to be handled different way.

Also I still find no other way to send the meta key combination without using KeyEvent(),
and only one meta key is allowed to be combined at a time for now.
This commit is contained in:
simonpunk 2020-06-13 12:34:08 +08:00
parent c331e2a418
commit fb189f338a
2 changed files with 81 additions and 2 deletions

View File

@ -25,6 +25,7 @@ import com.offsec.nhterm.emulatorview.compat.Patterns;
import java.io.IOException;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.List;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
@ -32,6 +33,7 @@ import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.res.Configuration;
import android.graphics.Canvas;
import android.graphics.Paint;
@ -60,6 +62,7 @@ import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.ExtractedText;
import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Scroller;
@ -179,6 +182,10 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe
private boolean mIsAltKeySent = false;
private boolean mIsFnKeySent = false;
// This is only used when user is using default system input method.
public boolean isCtrlPressed_defIM = false;
public boolean isUsingCustomInputMethod = false;
private boolean mMouseTracking;
private float mDensity;
@ -653,6 +660,11 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe
outAttrs.inputType = mUseCookedIme ?
EditorInfo.TYPE_CLASS_TEXT | mIMEInputType:
EditorInfo.TYPE_NULL;
// Everytime the user change the keyboard input method, reset the meta key status and check again the user keyboard input method.
switchOffAllMetaKey();
isUsingCustomInputMethod = isUsingCustomInputMethod();
return new BaseInputConnection(this, true) {
/**
* Used to handle composing text requests
@ -916,11 +928,27 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe
if (LOG_IME) {
Log.w(TAG, "sendKeyEvent(" + event + ")");
}
// Some keys are sent here rather than to commitText.
// In particular, del and the digit keys are sent here.
// (And I have reports that the HTC Magic also sends Return here.)
// As a bit of defensive programming, handle every key.
dispatchKeyEvent(event);
// So if user is using default system keyboard input method, sendKeyEvent() is called here.
// but somehow the key combination is not working through normal dispatchKeyEvent()
// the new KeyEvent class must be initialized with which meta key to use.
if (!isUsingCustomInputMethod) {
dispatchKeyEvent(new KeyEvent(
event.getDownTime(),
event.getEventTime(),
event.getAction(),
event.getKeyCode(),
event.getRepeatCount(),
chooseMetaKeyToUse()));
switchOffAllMetaKey();
} else {
dispatchKeyEvent(event);
}
return true;
}
@ -2270,4 +2298,38 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe
mIMEInputType = 0;
restartInput();
}
// function to choose which meta key to use, other meta keys may be added in the future.
public int chooseMetaKeyToUse() {
if (isCtrlPressed_defIM) return KeyEvent.META_CTRL_ON;
return 0;
}
// function to switch off all meta key status, other meta keys may be added in the future.
public void switchOffAllMetaKey() {
isCtrlPressed_defIM = false;
}
// function to check if the keyboard the user using is a default system keyboard input method
// default keyboard input method will call committext() function instead of sendKeyEvent() function,
// so it needs different way to handle the key input.
// https://stackoverflow.com/questions/8165618/how-to-check-if-the-native-hardware-keyboard-is-used
public boolean isUsingCustomInputMethod() {
InputMethodManager imm = (InputMethodManager) getRootView().getContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
List<InputMethodInfo> mInputMethodProperties = imm.getEnabledInputMethodList();
final int N = mInputMethodProperties.size();
for (int i = 0; i < N; i++) {
InputMethodInfo imi = mInputMethodProperties.get(i);
if (imi.getId().equals(
Settings.Secure.getString(getRootView().getContext().getContentResolver(),
Settings.Secure.DEFAULT_INPUT_METHOD))) {
if ((imi.getServiceInfo().applicationInfo.flags &
ApplicationInfo.FLAG_SYSTEM) == 0) {
return true;
}
}
}
return false;
}
}

View File

@ -28,6 +28,7 @@ import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Configuration;
@ -41,6 +42,7 @@ import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.DisplayMetrics;
@ -57,6 +59,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;
@ -309,6 +312,8 @@ public class Term extends Activity implements UpdateCallback, SharedPreferences.
*/
private View.OnKeyListener mKeyListener = new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (!getCurrentEmulatorView().isUsingCustomInputMethod)
setButtonToggledBackground(findViewById(R.id.button_ctrl), getCurrentEmulatorView().isCtrlPressed_defIM);
return backkeyInterceptor(keyCode, event) || keyboardShortcuts(keyCode, event);
}
@ -1654,6 +1659,13 @@ public class Term extends Activity implements UpdateCallback, SharedPreferences.
}
}
private void setButtonToggledBackground(View buttonView, Boolean toggled) {
if (toggled)
buttonView.getBackground().setAlpha(128);
else
buttonView.getBackground().setAlpha(255);
}
public void onClick(View v) {
EmulatorView view = getCurrentEmulatorView();
switch (v.getId()) {
@ -1661,7 +1673,12 @@ public class Term extends Activity implements UpdateCallback, SharedPreferences.
doSendActionBarKey(view, KeycodeConstants.KEYCODE_ESCAPE);
break;
case R.id.button_ctrl:
doSendActionBarKey(view, KeycodeConstants.KEYCODE_CTRL_LEFT);
if (view.isUsingCustomInputMethod) {
doSendActionBarKey(view, KeycodeConstants.KEYCODE_CTRL_LEFT);
} else {
view.isCtrlPressed_defIM = !view.isCtrlPressed_defIM;
setButtonToggledBackground(findViewById(R.id.button_ctrl), view.isCtrlPressed_defIM);
}
break;
case R.id.button_alt:
doSendActionBarKey(view, KeycodeConstants.KEYCODE_ALT_LEFT);