feat: 增加长按功能
This commit is contained in:
@@ -37,6 +37,9 @@ public class InputCommandHandler {
|
||||
case "SWIPE":
|
||||
handleSwipe(command);
|
||||
break;
|
||||
case "LONG_PRESS":
|
||||
handleLongPress(command);
|
||||
break;
|
||||
default:
|
||||
Log.w(TAG, "Unknown action: " + action);
|
||||
}
|
||||
@@ -82,6 +85,16 @@ public class InputCommandHandler {
|
||||
injectSwipe(x1, y1, x2, y2, duration);
|
||||
}
|
||||
|
||||
private void handleLongPress(JsonObject command) {
|
||||
float relX = command.get("x").getAsFloat();
|
||||
float relY = command.get("y").getAsFloat();
|
||||
int x = (int) (relX * screenWidth);
|
||||
int y = (int) (relY * screenHeight);
|
||||
|
||||
Log.d(TAG, "Long press at: " + x + ", " + y);
|
||||
injectLongPress(x, y);
|
||||
}
|
||||
|
||||
private void injectTap(int x, int y) {
|
||||
try {
|
||||
Runtime.getRuntime().exec("input tap " + x + " " + y);
|
||||
@@ -90,6 +103,15 @@ public class InputCommandHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private void injectLongPress(int x, int y) {
|
||||
try {
|
||||
// 用起点和终点相同的 swipe 模拟长按,持续 1000ms
|
||||
Runtime.getRuntime().exec("input swipe " + x + " " + y + " " + x + " " + y + " 1000");
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Error injecting long press event", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void injectSwipe(int x1, int y1, int x2, int y2, long duration) {
|
||||
try {
|
||||
Runtime.getRuntime().exec("input swipe " + x1 + " " + y1 + " " + x2 + " " + y2 + " " + duration);
|
||||
|
||||
Reference in New Issue
Block a user