1
0
Fork 0

Better support for hardware backspace (#281)

* DEL and CLEAR keys now work out-of-the-box, without extra configuration

* removed the DEL and CLEAR hotkey options
This commit is contained in:
Dimo Karaivanov 2023-06-07 11:27:36 +03:00 committed by GitHub
parent c63d054422
commit 2d57b71848
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 52 additions and 21 deletions

View file

@ -73,8 +73,6 @@
<string name="key_none" translatable="false">--</string>
<string name="key_back">Back</string>
<string name="key_call">Call</string>
<string name="key_clear" translatable="false">Clear</string>
<string name="key_delete" translatable="false">Delete</string>
<string name="key_menu" translatable="false">Menu</string>
<string name="key_soft_left" translatable="false">Left Func</string>
<string name="key_soft_right" translatable="false">Right Func</string>

View file

@ -118,10 +118,10 @@ abstract class KeyPadHandler extends InputMethodService {
// Logger.d("onKeyDown", "Key: " + event + " repeat?: " + event.getRepeatCount() + " long-time: " + event.isLongPress());
// "backspace" key must repeat its function, when held down, so we handle it in a special way
if (keyCode == settings.getKeyBackspace()) {
// "backspace" key must repeat its function when held down, so we handle it in a special way
if (Key.isBackspace(settings, keyCode)) {
// When there is no more text, allow "Back" key to function normally, not to block navigation.
// All other keys are blocked, unless it turns out it is annoying this way.
// All other keys have their default function disabled.
isBackspaceHandled = onBackspace() || keyCode != KeyEvent.KEYCODE_BACK;
return isBackspaceHandled;
} else {

View file

@ -23,6 +23,7 @@ import io.github.sspanak.tt9.ime.modes.InputMode;
import io.github.sspanak.tt9.languages.Language;
import io.github.sspanak.tt9.languages.LanguageCollection;
import io.github.sspanak.tt9.preferences.SettingsStore;
import io.github.sspanak.tt9.preferences.helpers.Hotkeys;
import io.github.sspanak.tt9.ui.UI;
import io.github.sspanak.tt9.ui.main.MainView;
import io.github.sspanak.tt9.ui.tray.StatusBar;
@ -88,7 +89,7 @@ public class TraditionalT9 extends KeyPadHandler {
private void validateFunctionKeys() {
if (settings.isSettingsKeyMissing()) {
settings.setDefaultKeys();
Hotkeys.setDefault(settings);
}
}

View file

@ -5,6 +5,14 @@ import android.view.KeyEvent;
import io.github.sspanak.tt9.preferences.SettingsStore;
public class Key {
public static boolean isBackspace(SettingsStore settings, int keyCode) {
return
keyCode == KeyEvent.KEYCODE_DEL
|| keyCode == KeyEvent.KEYCODE_CLEAR
|| keyCode == settings.getKeyBackspace();
}
public static boolean isNumber(int keyCode) {
return
(keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9)

View file

@ -18,6 +18,7 @@ import io.github.sspanak.tt9.db.DictionaryDb;
import io.github.sspanak.tt9.db.DictionaryLoader;
import io.github.sspanak.tt9.ime.helpers.GlobalKeyboardSettings;
import io.github.sspanak.tt9.ime.helpers.InputModeValidator;
import io.github.sspanak.tt9.preferences.helpers.Hotkeys;
import io.github.sspanak.tt9.preferences.screens.AppearanceScreen;
import io.github.sspanak.tt9.preferences.screens.DictionariesScreen;
import io.github.sspanak.tt9.preferences.screens.HotkeysScreen;
@ -143,7 +144,7 @@ public class PreferencesActivity extends AppCompatActivity implements Preference
private void validateFunctionKeys() {
if (settings.isSettingsKeyMissing()) {
settings.setDefaultKeys();
Hotkeys.setDefault(settings);
}
}

View file

@ -2,7 +2,6 @@ package io.github.sspanak.tt9.preferences;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.KeyEvent;
import androidx.preference.PreferenceManager;
@ -161,12 +160,12 @@ public class SettingsStore {
return getKeyShowSettings() == 0;
}
public void setDefaultKeys() {
prefsEditor.putString(SectionKeymap.ITEM_ADD_WORD, String.valueOf(KeyEvent.KEYCODE_STAR));
prefsEditor.putString(SectionKeymap.ITEM_BACKSPACE, String.valueOf(KeyEvent.KEYCODE_BACK));
prefsEditor.putString(SectionKeymap.ITEM_NEXT_INPUT_MODE, String.valueOf(KeyEvent.KEYCODE_POUND));
prefsEditor.putString(SectionKeymap.ITEM_NEXT_LANGUAGE, String.valueOf(-KeyEvent.KEYCODE_POUND));
prefsEditor.putString(SectionKeymap.ITEM_SHOW_SETTINGS, String.valueOf(-KeyEvent.KEYCODE_STAR));
public void setDefaultKeys(int addWord, int backspace, int nextInputMode, int nextLanguage, int showSettings) {
prefsEditor.putString(SectionKeymap.ITEM_ADD_WORD, String.valueOf(addWord));
prefsEditor.putString(SectionKeymap.ITEM_BACKSPACE, String.valueOf(backspace));
prefsEditor.putString(SectionKeymap.ITEM_NEXT_INPUT_MODE, String.valueOf(nextInputMode));
prefsEditor.putString(SectionKeymap.ITEM_NEXT_LANGUAGE, String.valueOf(nextLanguage));
prefsEditor.putString(SectionKeymap.ITEM_SHOW_SETTINGS, String.valueOf(showSettings));
prefsEditor.apply();
}
@ -181,19 +180,15 @@ public class SettingsStore {
public int getKeyAddWord() {
return getFunctionKey(SectionKeymap.ITEM_ADD_WORD);
}
public int getKeyBackspace() {
return getFunctionKey(SectionKeymap.ITEM_BACKSPACE);
}
public int getKeyNextInputMode() {
return getFunctionKey(SectionKeymap.ITEM_NEXT_INPUT_MODE);
}
public int getKeyNextLanguage() {
return getFunctionKey(SectionKeymap.ITEM_NEXT_LANGUAGE);
}
public int getKeyShowSettings() {
return getFunctionKey(SectionKeymap.ITEM_SHOW_SETTINGS);
}

View file

@ -10,6 +10,7 @@ import java.util.LinkedHashMap;
import java.util.Set;
import io.github.sspanak.tt9.R;
import io.github.sspanak.tt9.preferences.SettingsStore;
public class Hotkeys {
private final Context context;
@ -39,6 +40,34 @@ public class Hotkeys {
}
/**
* setDefault
* Applies the default hotkey scheme.
* When a standard "Backspace" hardware key is available, "Backspace" hotkey association is not necessary,
* so it will be left out blank, to allow the hardware key do its job.
* When the on-screen keyboard is on, "Back" is also not associated, because it will cause weird user
* experience. Instead the on-screen "Backspace" key can be used.
*/
public static void setDefault(SettingsStore settings) {
int backspaceKeyCode = KeyEvent.KEYCODE_BACK;
if (
KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_CLEAR)
|| KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_DEL)
|| settings.getShowSoftNumpad()
) {
backspaceKeyCode = 0;
}
settings.setDefaultKeys(
KeyEvent.KEYCODE_STAR,
backspaceKeyCode,
KeyEvent.KEYCODE_POUND,
-KeyEvent.KEYCODE_POUND,
-KeyEvent.KEYCODE_STAR
);
}
/**
* addIfDeviceHasKey
* Add the key only if Android says the device has such keypad button or a permanent touch key.
@ -107,8 +136,6 @@ public class Hotkeys {
add(KeyEvent.KEYCODE_CALL, R.string.key_call, true);
addIfDeviceHasKey(KeyEvent.KEYCODE_BACK, R.string.key_back, false);
addIfDeviceHasKey(KeyEvent.KEYCODE_CLEAR, R.string.key_clear, false);
addIfDeviceHasKey(KeyEvent.KEYCODE_DEL, R.string.key_delete, false);
addIfDeviceHasKey(KeyEvent.KEYCODE_F1, "F1", true);
addIfDeviceHasKey(KeyEvent.KEYCODE_F2, "F2", true);
addIfDeviceHasKey(KeyEvent.KEYCODE_F3, "F3", true);

View file

@ -6,6 +6,7 @@ import androidx.preference.Preference;
import io.github.sspanak.tt9.R;
import io.github.sspanak.tt9.preferences.SettingsStore;
import io.github.sspanak.tt9.preferences.helpers.Hotkeys;
import io.github.sspanak.tt9.ui.UI;
@ -26,7 +27,7 @@ public class ItemResetKeys extends ItemClickable {
@Override
protected boolean onClick(Preference p) {
settings.setDefaultKeys();
Hotkeys.setDefault(settings);
dropdowns.reloadSettings();
UI.toast(context, R.string.function_reset_keys_done);
return true;