fix: 增加按键传输
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user