Skip to content

Commit

Permalink
OpenXR - Software keyboard mapping fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
lvonasek committed Sep 19, 2024
1 parent a99871e commit 17a9345
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 70 deletions.
88 changes: 20 additions & 68 deletions app/src/main/java/com/winlator/XrActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Display;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

Expand Down Expand Up @@ -42,6 +44,7 @@ public enum ControllerButton {
private static boolean isDeviceSupported = false;
private static boolean isImmersive = false;
private static boolean isSBS = false;
private static final KeyCharacterMap chars = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
private static final float[] lastAxes = new float[ControllerAxis.values().length];
private static final boolean[] lastButtons = new boolean[ControllerButton.values().length];
private static String lastText = "";
Expand Down Expand Up @@ -82,65 +85,27 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {}

@Override
public synchronized void afterTextChanged(Editable e) {
XServer server = instance.getXServer();
EditText text = findViewById(R.id.XRTextInput);
String s = text.getEditableText().toString();
if (s.length() > lastText.length()) {
lastText = s;
char c = s.charAt(s.length() - 1);
boolean uppercase = c >= 'A' && c <= 'Z';
if (uppercase) {
c = (char)((int)c - (int)'A' + (int)'a');
}

switch (c) {
case '0': setKeyPressed(XKeycode.KEY_0, uppercase); break;
case '1': setKeyPressed(XKeycode.KEY_1, uppercase); break;
case '2': setKeyPressed(XKeycode.KEY_2, uppercase); break;
case '3': setKeyPressed(XKeycode.KEY_3, uppercase); break;
case '4': setKeyPressed(XKeycode.KEY_4, uppercase); break;
case '5': setKeyPressed(XKeycode.KEY_5, uppercase); break;
case '6': setKeyPressed(XKeycode.KEY_6, uppercase); break;
case '7': setKeyPressed(XKeycode.KEY_7, uppercase); break;
case '8': setKeyPressed(XKeycode.KEY_8, uppercase); break;
case '9': setKeyPressed(XKeycode.KEY_9, uppercase); break;

case 'a': setKeyPressed(XKeycode.KEY_A, uppercase); break;
case 'b': setKeyPressed(XKeycode.KEY_B, uppercase); break;
case 'c': setKeyPressed(XKeycode.KEY_C, uppercase); break;
case 'd': setKeyPressed(XKeycode.KEY_D, uppercase); break;
case 'e': setKeyPressed(XKeycode.KEY_E, uppercase); break;
case 'f': setKeyPressed(XKeycode.KEY_F, uppercase); break;
case 'g': setKeyPressed(XKeycode.KEY_G, uppercase); break;
case 'h': setKeyPressed(XKeycode.KEY_H, uppercase); break;
case 'i': setKeyPressed(XKeycode.KEY_I, uppercase); break;
case 'j': setKeyPressed(XKeycode.KEY_J, uppercase); break;
case 'k': setKeyPressed(XKeycode.KEY_K, uppercase); break;
case 'l': setKeyPressed(XKeycode.KEY_L, uppercase); break;
case 'm': setKeyPressed(XKeycode.KEY_M, uppercase); break;
case 'n': setKeyPressed(XKeycode.KEY_N, uppercase); break;
case 'o': setKeyPressed(XKeycode.KEY_O, uppercase); break;
case 'p': setKeyPressed(XKeycode.KEY_P, uppercase); break;
case 'q': setKeyPressed(XKeycode.KEY_Q, uppercase); break;
case 'r': setKeyPressed(XKeycode.KEY_R, uppercase); break;
case 's': setKeyPressed(XKeycode.KEY_S, uppercase); break;
case 't': setKeyPressed(XKeycode.KEY_T, uppercase); break;
case 'u': setKeyPressed(XKeycode.KEY_U, uppercase); break;
case 'v': setKeyPressed(XKeycode.KEY_V, uppercase); break;
case 'w': setKeyPressed(XKeycode.KEY_W, uppercase); break;
case 'x': setKeyPressed(XKeycode.KEY_X, uppercase); break;
case 'y': setKeyPressed(XKeycode.KEY_Y, uppercase); break;
case 'z': setKeyPressed(XKeycode.KEY_Z, uppercase); break;

case ' ': setKeyPressed(XKeycode.KEY_SPACE, uppercase); break;
case '.': setKeyPressed(XKeycode.KEY_PERIOD, uppercase); break;
case ',': setKeyPressed(XKeycode.KEY_COMMA, uppercase); break;
KeyEvent[] events = chars.getEvents(new char[]{s.charAt(s.length() - 1)});
if (events != null) {
for (KeyEvent keyEvent : events) {
server.keyboard.onKeyEvent(keyEvent);
sleep(50);
}
}
}
else {
} else {
lastText = s;
setKeyPressed(XKeycode.KEY_BKSP, false);
server.keyboard.onKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
sleep(50);
server.keyboard.onKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
}
if (s.isEmpty()) {
resetText();
}
resetText();
}

private synchronized void resetText() {
Expand Down Expand Up @@ -334,25 +299,12 @@ private static void mapKey(ControllerButton xrButton, byte xKeycode) {
}
}

private static void setKeyPressed(XKeycode xKeycode, boolean uppercase) {
XServer server = instance.getXServer();
if (uppercase) {
server.injectKeyPress(XKeycode.KEY_SHIFT_L);
}
server.injectKeyPress(xKeycode);

// Give the system bit of time to notice the key press
private static void sleep(int ms) {
try {
Thread.sleep(50);
}
catch (Exception e) {
Thread.sleep(ms);
} catch (InterruptedException e) {
e.printStackTrace();
}

server.injectKeyRelease(xKeycode);
if (uppercase) {
server.injectKeyRelease(XKeycode.KEY_SHIFT_L);
}
}

// Rendering
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/layout/xserver_display_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
android:id="@+id/XRTextInput"
android:layout_width="0dp"
android:layout_height="0dp"
android:focusableInTouchMode="true"
android:inputType="text"
android:inputType="textMultiLine"
android:autofillHints="no"
tools:ignore="LabelFor"
android:visibility="gone" />
Expand Down

0 comments on commit 17a9345

Please sign in to comment.