Skip to content

Commit

Permalink
Implement keyboard shortcut to switch profiles #4
Browse files Browse the repository at this point in the history
  • Loading branch information
Xtr126 committed Feb 17, 2023
1 parent 467c664 commit cd952c0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
55 changes: 33 additions & 22 deletions app/src/main/java/xtr/keymapper/TouchPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import static xtr.keymapper.InputEventCodes.REL_WHEEL;
import static xtr.keymapper.InputEventCodes.REL_X;
import static xtr.keymapper.InputEventCodes.REL_Y;
import static xtr.keymapper.KeymapConfig.KEY_ALT;
import static xtr.keymapper.KeymapConfig.KEY_CTRL;
import static xtr.keymapper.TouchPointer.PointerId.dpad1pid;
import static xtr.keymapper.TouchPointer.PointerId.dpad2pid;
import static xtr.keymapper.TouchPointer.PointerId.pid1;
import static xtr.keymapper.server.InputService.DOWN;
import static xtr.keymapper.server.InputService.MOVE;
import static xtr.keymapper.server.InputService.UP;
import static xtr.keymapper.server.InputService.reloadKeymap;

import android.app.Notification;
import android.app.NotificationChannel;
Expand Down Expand Up @@ -204,11 +207,6 @@ public void loadKeymap(String profileName) {
keymapConfig = new KeymapConfig(this);
mouseEventHandler.sensitivity = keymapConfig.mouseSensitivity.intValue();
mouseEventHandler.scroll_speed_multiplier = keymapConfig.scrollSpeed.intValue();
mouseEventHandler.ctrl_mouse_wheel_zoom = keymapConfig.ctrlMouseWheelZoom;
mouseEventHandler.ctrl_mouse_drag_gesture = keymapConfig.ctrlDragMouseGesture;

keyEventHandler.stop_service = keymapConfig.stopServiceShortcutKey;
keyEventHandler.launch_editor = keymapConfig.launchEditorShortcutKey;
}

public void sendSettingstoServer() throws RemoteException {
Expand Down Expand Up @@ -288,7 +286,7 @@ private void getDisplayMetrics() {

private class KeyEventHandler {
boolean ctrlKeyPressed = false;
int stop_service, launch_editor;
boolean altKeyPressed = false;

private void init() {
if (dpad1Handler != null) dpad1Handler.setInterface(mService);
Expand Down Expand Up @@ -321,17 +319,13 @@ private void handleEvent(String line) throws RemoteException {
}
if (activityCallback != null) activityCallback.updateCmdView2(line + "\n");


// Handle keyboard shortcuts
int i = Utils.obtainIndex(event.code);
if (i > 0) { // A-Z and 0-9 keys
if (event.action == DOWN) {
if (i == stop_service) stopPointer();
if (i == launch_editor)
startService(new Intent(TouchPointer.this, EditorService.class));
}
if (event.action == DOWN) handleKeyboardShortcuts(i);

if (dpad2Handler != null) // Dpad with WASD keys
dpad2Handler.handleEvent(event.code, event.action);

} else { // CTRL, ALT, Arrow keys
if (dpad1Handler != null) // Dpad with arrow keys
dpad1Handler.handleEvent(event.code, event.action);
Expand All @@ -340,19 +334,35 @@ private void handleEvent(String line) throws RemoteException {
mouseEventHandler.triggerMouseAim();
}
if (event.code.contains("CTRL")) ctrlKeyPressed = event.action == DOWN;
if (event.code.contains("ALT")) altKeyPressed = event.action == DOWN;

for (int n = 0; n < keyList.size(); n++) {
KeymapProfiles.Key key = keyList.get(n);
if (event.code.equals(key.code))
mService.injectEvent(key.x, key.y, event.action, n);
}
}

private void handleKeyboardShortcuts(int keycode) {
final String modifier = ctrlKeyPressed ? KEY_CTRL : KEY_ALT;

if (keymapConfig.launchEditorShortcutKeyModifier.equals(modifier))
if (keycode == keymapConfig.launchEditorShortcutKey)
startService(new Intent(TouchPointer.this, EditorService.class));

if (keymapConfig.stopServiceShortcutKeyModifier.equals(modifier))
if (keycode == keymapConfig.stopServiceShortcutKey)
stopPointer();

if (keymapConfig.switchProfileShortcutKeyModifier.equals(modifier))
if (keycode == keymapConfig.switchProfileShortcutKey)
reloadKeymap();
}
}

private class MouseEventHandler {
int sensitivity = 1;
int scroll_speed_multiplier = 1;
boolean ctrl_mouse_wheel_zoom, ctrl_mouse_drag_gesture;
private MousePinchZoom pinchZoom;
private MouseWheelZoom scrollZoomHandler;
private final int pointerId = pid1.id;
Expand All @@ -373,7 +383,8 @@ private void init() {
mouseAimHandler.setInterface(mService);
mouseAimHandler.setDimensions(width, height);
}
if (ctrl_mouse_wheel_zoom) scrollZoomHandler = new MouseWheelZoom(mService);
if (keymapConfig.ctrlMouseWheelZoom)
scrollZoomHandler = new MouseWheelZoom(mService);
}

private void movePointer() { mHandler.post(() -> {
Expand All @@ -388,11 +399,11 @@ private void handleEvent(int code, int value) throws RemoteException {
mouseAimHandler.handleEvent(code, value);
return;
}
if (keyEventHandler.ctrlKeyPressed && pointer_down && ctrl_mouse_drag_gesture) {
pointer_down = pinchZoom.handleEvent(code, value);
return;
}

if (keyEventHandler.ctrlKeyPressed && pointer_down)
if (keymapConfig.ctrlDragMouseGesture) {
pointer_down = pinchZoom.handleEvent(code, value);
return;
}
switch (code) {
case REL_X: {
if (value == 0) break;
Expand All @@ -414,7 +425,7 @@ private void handleEvent(int code, int value) throws RemoteException {
}
case BTN_MOUSE:
pointer_down = value == 1;
if (keyEventHandler.ctrlKeyPressed && ctrl_mouse_drag_gesture) {
if (keyEventHandler.ctrlKeyPressed && keymapConfig.ctrlDragMouseGesture) {
pinchZoom = new MousePinchZoom(mService, x1, y1);
pinchZoom.handleEvent(code, value);
} else mService.injectEvent(x1, y1, value, pointerId);
Expand All @@ -425,7 +436,7 @@ private void handleEvent(int code, int value) throws RemoteException {
break;

case REL_WHEEL:
if (keyEventHandler.ctrlKeyPressed && ctrl_mouse_wheel_zoom)
if (keyEventHandler.ctrlKeyPressed && keymapConfig.ctrlMouseWheelZoom)
scrollZoomHandler.onScrollEvent(value, x1, y1);
else
mService.injectScroll(x1, y1, value * scroll_speed_multiplier);
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/res/layout/fragment_settings_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:theme="@style/Theme.Material3.DynamicColors.Dark"
android:padding="20dp">
Expand Down Expand Up @@ -189,8 +189,7 @@

<com.google.android.material.button.MaterialButtonToggleGroup
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1">
android:layout_height="wrap_content">
<com.google.android.material.button.MaterialButton
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
Expand Down

0 comments on commit cd952c0

Please sign in to comment.