Skip to content

Commit

Permalink
Stop macro on key press
Browse files Browse the repository at this point in the history
  • Loading branch information
Xtr126 committed Nov 23, 2024
1 parent 1f988b0 commit e92a8d1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/xtr/keymapper/editor/EditorUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 12 additions & 7 deletions app/src/main/java/xtr/keymapper/macro/MacroView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -39,7 +41,6 @@ public MacroView(Context context, OnFinishListener onFinishListener) {

// Set up path
path = new Path();

}

@Override
Expand All @@ -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();
Expand All @@ -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;
}
Expand Down

0 comments on commit e92a8d1

Please sign in to comment.