From e92a8d1ea40785b2c0e2e8d486418fbda4f061fc Mon Sep 17 00:00:00 2001 From: Xtr126 <80520774+Xtr126@users.noreply.github.com> Date: Sat, 23 Nov 2024 16:38:37 +0530 Subject: [PATCH] Stop macro on key press --- .../java/xtr/keymapper/editor/EditorUI.java | 2 ++ .../java/xtr/keymapper/macro/MacroView.java | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/xtr/keymapper/editor/EditorUI.java b/app/src/main/java/xtr/keymapper/editor/EditorUI.java index 460dec3..d736dbd 100644 --- a/app/src/main/java/xtr/keymapper/editor/EditorUI.java +++ b/app/src/main/java/xtr/keymapper/editor/EditorUI.java @@ -204,8 +204,10 @@ else if (id == R.id.mouse_right) { private void addMacro() { MacroView macroView = new MacroView(context, (view, savedState) -> { mainView.removeView(view); + mainView.setOnKeyListener(this::onKey); }); mainView.addView(macroView); + mainView.setOnKeyListener((v, keyCode, event) -> macroView.onKey(keyCode, event)); } public interface OnHideListener { diff --git a/app/src/main/java/xtr/keymapper/macro/MacroView.java b/app/src/main/java/xtr/keymapper/macro/MacroView.java index 298ed76..2f22880 100644 --- a/app/src/main/java/xtr/keymapper/macro/MacroView.java +++ b/app/src/main/java/xtr/keymapper/macro/MacroView.java @@ -6,6 +6,8 @@ import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; +import android.view.InputDevice; +import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; @@ -17,7 +19,7 @@ public class MacroView extends View { private final Paint paintOuter; private final Paint paintInner; private final Path path; - private StringBuilder stringBuilder; + private final StringBuilder stringBuilder = new StringBuilder(); private final OnFinishListener onFinishListener; public MacroView(Context context, OnFinishListener onFinishListener) { @@ -39,7 +41,6 @@ public MacroView(Context context, OnFinishListener onFinishListener) { // Set up path path = new Path(); - } @Override @@ -49,6 +50,14 @@ protected void onDraw(@NonNull Canvas canvas) { canvas.drawPath(path, paintInner); // Draw the path } + public boolean onKey(int keyCode, KeyEvent event) { + if (event.getSource() == InputDevice.SOURCE_KEYBOARD) { + clearCanvas(); + onFinishListener.onFinishMacro(this, stringBuilder.toString()); + } + return super.onKeyUp(keyCode, event); + } + @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); @@ -57,16 +66,12 @@ public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: - stringBuilder = new StringBuilder(); path.moveTo(x, y); // Start new path case MotionEvent.ACTION_MOVE: path.lineTo(x, y); // Draw line to current touch point break; case MotionEvent.ACTION_UP: - onFinishListener.onFinishMacro(this, stringBuilder.toString()); - stringBuilder = null; - clearCanvas(); - return true; + path.moveTo(x, y); // Start new path default: return false; }