Clean up, keyboard is faster now

This commit is contained in:
SerpentSpirale 2021-07-07 14:46:28 +02:00
parent 22b3583ae1
commit 153e991806
3 changed files with 49 additions and 54 deletions

View file

@ -624,8 +624,10 @@ public class BaseMainActivity extends LoggableActivity {
return true;
}
if(isKeyboard(event)) {
EfficientAndroidLWJGLKeycode.execKey(event,event.getKeyCode(),event.getAction() == KeyEvent.ACTION_DOWN);
int index = EfficientAndroidLWJGLKeycode.getIndexByKey(event.getKeyCode());
if(index >= 0) {
Toast.makeText(this,"THIS IS A KEYBOARD EVENT !", Toast.LENGTH_SHORT).show();
EfficientAndroidLWJGLKeycode.execKey(event, index);
return true;
}
@ -734,7 +736,7 @@ public class BaseMainActivity extends LoggableActivity {
private void dialogSendCustomKey() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle(R.string.control_customkey);
dialog.setItems(EfficientAndroidLWJGLKeycode.generateKeyName(), (dInterface, position) -> EfficientAndroidLWJGLKeycode.execKeyIndex(BaseMainActivity.this, position));
dialog.setItems(EfficientAndroidLWJGLKeycode.generateKeyName(), (dInterface, position) -> EfficientAndroidLWJGLKeycode.execKeyIndex(position));
dialog.show();
}
@ -878,8 +880,8 @@ public class BaseMainActivity extends LoggableActivity {
if(doesObjectContainField(KeyEvent.class,"KEYCODE_" + Character.toUpperCase(keyChar))) {
try {
int keyCode = KeyEvent.class.getField("KEYCODE_" + Character.toUpperCase(keyChar)).getInt(null);
sendKeyPress(EfficientAndroidLWJGLKeycode.get(keyCode), keyChar, 0, CallbackBridge.getCurrentMods(), true);
sendKeyPress(EfficientAndroidLWJGLKeycode.get(keyCode), keyChar, 0, CallbackBridge.getCurrentMods(), false);
sendKeyPress(EfficientAndroidLWJGLKeycode.getValue(keyCode), keyChar, 0, CallbackBridge.getCurrentMods(), true);
sendKeyPress(EfficientAndroidLWJGLKeycode.getValue(keyCode), keyChar, 0, CallbackBridge.getCurrentMods(), false);
} catch (IllegalAccessException | NoSuchFieldException e) {
}
@ -890,7 +892,7 @@ public class BaseMainActivity extends LoggableActivity {
sendKeyPress(0, keyChar, 0, CallbackBridge.getCurrentMods(), false);
}
public void sendKeyPress(int keyCode) {
public static void sendKeyPress(int keyCode) {
sendKeyPress(keyCode, CallbackBridge.getCurrentMods(), true);
sendKeyPress(keyCode, CallbackBridge.getCurrentMods(), false);
}

View file

@ -6,7 +6,14 @@ import net.kdt.pojavlaunch.prefs.LauncherPreferences;
import org.lwjgl.glfw.CallbackBridge;
import java.util.Arrays;
public class EfficientAndroidLWJGLKeycode {
//This old version of this class was using an ArrayMap, a generic Key -> Value data structure.
//The key being the android keycode from a KeyEvent
//The value its LWJGL equivalent.
private static final int[] androidKeycodes = new int[103];
private static final short[] LWJGLKeycodes = new short[androidKeycodes.length];
private static String[] androidKeyNameArray; /* = new String[androidKeycodes.length]; */
@ -155,36 +162,13 @@ public class EfficientAndroidLWJGLKeycode {
++index;
}
public static short get(int key){
//Taken from: https://www.geeksforgeeks.org/binary-search/
//Give the value associated to a key
int left = 0, right = androidKeycodes.length - 1;
while (left <= right) {
int m = left + (right - left) / 2;
// Check if x is present at mid
if (androidKeycodes[m] == key)
return LWJGLKeycodes[m];
// If x greater, ignore left half
if (androidKeycodes[m] < key)
left = m + 1;
// If x is smaller, ignore right half
else
right = m - 1;
}
// if we reach here, then element was
// not present
return -1;
}
public static boolean containsKey(int keycode){
return get(keycode) != -1;
return getIndexByKey(keycode) >= 0;
}
public static String[] generateKeyName() {
if (androidKeyNameArray == null) {
androidKeyNameArray = new String[androidKeycodes.length];
@ -195,7 +179,13 @@ public class EfficientAndroidLWJGLKeycode {
return androidKeyNameArray;
}
public static void execKey(KeyEvent keyEvent, int i, boolean isDown) {
public static void execKey(KeyEvent keyEvent) {
execKey(keyEvent, getIndexByKey(keyEvent.getKeyCode()));
}
public static void execKey(KeyEvent keyEvent, int valueIndex) {
//valueIndex points to where the value is stored in the array.
CallbackBridge.holdingAlt = keyEvent.isAltPressed();
CallbackBridge.holdingCapslock = keyEvent.isCapsLockOn();
CallbackBridge.holdingCtrl = keyEvent.isCtrlPressed();
@ -203,40 +193,43 @@ public class EfficientAndroidLWJGLKeycode {
CallbackBridge.holdingShift = keyEvent.isShiftPressed();
try {
System.out.println(keyEvent.getKeyCode() + " " +keyEvent.getDisplayLabel());
System.out.println(keyEvent.getKeyCode() + " " +keyEvent.getDisplayLabel());
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_BACK && LauncherPreferences.PREF_BACK_TO_RIGHT_MOUSE) {
BaseMainActivity.sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, keyEvent.getAction() == KeyEvent.ACTION_DOWN);
} else {
if(keyEvent.getUnicodeChar() != 0) {
char key = (char)keyEvent.getUnicodeChar();
BaseMainActivity.sendKeyPress(
get(keyEvent.getKeyCode()),
key,
0,
CallbackBridge.getCurrentMods(),
keyEvent.getAction() == KeyEvent.ACTION_DOWN);
}else{
BaseMainActivity.sendKeyPress(
get(keyEvent.getKeyCode()),
CallbackBridge.getCurrentMods(),
keyEvent.getAction()==KeyEvent.ACTION_DOWN);
}
char key = (char)(keyEvent.getUnicodeChar() != 0 ? keyEvent.getUnicodeChar() : '\u0000');
BaseMainActivity.sendKeyPress(
getValueByIndex(valueIndex),
key,
0,
CallbackBridge.getCurrentMods(),
keyEvent.getAction() == KeyEvent.ACTION_DOWN);
}
} catch (Throwable th) {
th.printStackTrace();
}
}
public static void execKeyIndex(BaseMainActivity mainActivity, int index) {
mainActivity.sendKeyPress(getKeyByIndex(index));
public static void execKeyIndex(int index){
//Send a quick key press.
BaseMainActivity.sendKeyPress(getValueByIndex(index));
}
public static int getKeyByIndex(int index) {
public static int getValueByIndex(int index) {
return LWJGLKeycodes[index];
}
public static int getIndexByLWJGLKey(int lwjglKey) {
public static int getIndexByKey(int key){
return Arrays.binarySearch(androidKeycodes, key);
}
public static short getValue(int key){
return LWJGLKeycodes[Arrays.binarySearch(androidKeycodes, key)];
}
public static int getIndexByValue(int lwjglKey) {
//Since the LWJGL keycodes aren't sorted, linear search is used.
//You should avoid using this function on performance critical areas
for (int i = 0; i < LWJGLKeycodes.length; i++) {
if(LWJGLKeycodes[i] == lwjglKey) return i;
}

View file

@ -234,7 +234,7 @@ public class EditControlButtonPopup {
if (properties.keycodes[i] < 0) {
spinnersKeycode[i].setSelection(properties.keycodes[i] + specialArr.length);
} else {
spinnersKeycode[i].setSelection(EfficientAndroidLWJGLKeycode.getIndexByLWJGLKey(properties.keycodes[i]) + specialArr.length);
spinnersKeycode[i].setSelection(EfficientAndroidLWJGLKeycode.getIndexByValue(properties.keycodes[i]) + specialArr.length);
}
}
}
@ -272,7 +272,7 @@ public class EditControlButtonPopup {
if (spinnersKeycode[i].getSelectedItemPosition() < specialArr.length) {
properties.keycodes[i] = spinnersKeycode[i].getSelectedItemPosition() - specialArr.length;
} else {
properties.keycodes[i] = EfficientAndroidLWJGLKeycode.getKeyByIndex(spinnersKeycode[i].getSelectedItemPosition() - specialArr.length);
properties.keycodes[i] = EfficientAndroidLWJGLKeycode.getValueByIndex(spinnersKeycode[i].getSelectedItemPosition() - specialArr.length);
}
}