From 174a977b0f9c41e9501523e5b196895bba45b1c7 Mon Sep 17 00:00:00 2001 From: tongtongstudio Date: Tue, 14 Jul 2026 00:57:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A2=9E=E5=8A=A0=E6=8C=89=E9=94=AE?= =?UTF-8?q?=E4=BC=A0=E8=BE=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/tt/controller/MainActivity.java | 16 +++++++++++ .../tt/controller/view/RemoteTouchView.java | 27 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/WebRTCController/app/src/main/java/com/tt/controller/MainActivity.java b/WebRTCController/app/src/main/java/com/tt/controller/MainActivity.java index 68886ce..721a6a2 100644 --- a/WebRTCController/app/src/main/java/com/tt/controller/MainActivity.java +++ b/WebRTCController/app/src/main/java/com/tt/controller/MainActivity.java @@ -118,6 +118,11 @@ public class MainActivity extends AppCompatActivity { public void onSwipe(float relX1, float relY1, float relX2, float relY2, long duration) { sendSwipeCommand(relX1, relY1, relX2, relY2, duration); } + + @Override + public void onKeyEvent(int keyCode, int action) { + sendKeyCommand(keyCode, action); + } }); updateUI(false); @@ -224,6 +229,7 @@ public class MainActivity extends AppCompatActivity { runOnUiThread(() -> { tvStatus.setText("状态: 已连接 - 远程控制中"); updateUI(true); + touchOverlay.requestFocus(); }); } @@ -311,6 +317,16 @@ public class MainActivity extends AppCompatActivity { } } + private void sendKeyCommand(int keyCode, int action) { + if (webRtcClient != null) { + JsonObject cmd = new JsonObject(); + cmd.addProperty("action", "KEY"); + cmd.addProperty("keyCode", keyCode); + cmd.addProperty("keyAction", action); + webRtcClient.sendControlCommand(cmd.toString()); + } + } + private void disconnect() { if (webRtcClient != null) { webRtcClient.close(); diff --git a/WebRTCController/app/src/main/java/com/tt/controller/view/RemoteTouchView.java b/WebRTCController/app/src/main/java/com/tt/controller/view/RemoteTouchView.java index b83b2a4..2093d41 100644 --- a/WebRTCController/app/src/main/java/com/tt/controller/view/RemoteTouchView.java +++ b/WebRTCController/app/src/main/java/com/tt/controller/view/RemoteTouchView.java @@ -29,24 +29,51 @@ public class RemoteTouchView extends SurfaceView { public interface TouchEventListener { void onTouch(float relX, float relY); void onSwipe(float relX1, float relY1, float relX2, float relY2, long duration); + void onKeyEvent(int keyCode, int action); } public RemoteTouchView(Context context) { super(context); + init(); } public RemoteTouchView(Context context, AttributeSet attrs) { super(context, attrs); + init(); } public RemoteTouchView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); + init(); + } + + private void init() { + setFocusable(true); + setFocusableInTouchMode(true); } public void setTouchEventListener(TouchEventListener listener) { this.listener = listener; } + @Override + public boolean onKeyDown(int keyCode, android.view.KeyEvent event) { + if (listener != null) { + listener.onKeyEvent(keyCode, android.view.KeyEvent.ACTION_DOWN); + return true; + } + return super.onKeyDown(keyCode, event); + } + + @Override + public boolean onKeyUp(int keyCode, android.view.KeyEvent event) { + if (listener != null) { + listener.onKeyEvent(keyCode, android.view.KeyEvent.ACTION_UP); + return true; + } + return super.onKeyUp(keyCode, event); + } + @Override public boolean onTouchEvent(MotionEvent event) { if (listener == null) return super.onTouchEvent(event);