Browse Source

send enter

gesture
Backpack 2 months ago
parent
commit
d1691ae0cb
4 changed files with 95 additions and 1 deletions
  1. BIN
      wavecontrol-test/src-py/router/__pycache__/ws.cpython-312.pyc
  2. +65
    -0
      wavecontrol-test/src-py/router/ws.py
  3. +29
    -0
      wavecontrol-test/src/hand_landmark/gesture_handler.ts
  4. +1
    -1
      wavecontrol-test/src/store/app.ts

BIN
wavecontrol-test/src-py/router/__pycache__/ws.cpython-312.pyc View File


+ 65
- 0
wavecontrol-test/src-py/router/ws.py View File

@ -51,6 +51,9 @@ class WebSocketMessageType:
MOUSE_MOVE = "mouse_move"
MOUSE_CLICK = "mouse_click"
# 键盘操作类型
SEND_KEYS = "send_keys"
class MessageSender:
"""消息发送器,发送给前端"""
@ -97,6 +100,65 @@ class GestureHandler:
print("👉 正在点击鼠标")
self.mouse.click(Button.left)
def send_keys(self, key_str: str) -> None:
"""
Args:
key_str: 'ctrl+r' 'F11'
"""
try:
keys = [self.__parse_key(key) for key in key_str.split("+")]
print("👉 正在点击发送案按键")
self.__execute_keys(keys)
except Exception as e:
print(f"发送按键失败: {e}")
def __parse_key(self, key_str: str) -> Union[str, Key]:
"""
Args:
key_str:
Returns:
"""
key_str = key_str.strip().lower()
if hasattr(Key, key_str):
return getattr(Key, key_str)
elif len(key_str) == 1:
return key_str
elif key_str.startswith("f"):
try:
return getattr(Key, key_str)
except AttributeError:
raise ValueError(f"无效的功能键: {key_str}")
else:
raise ValueError(f"无效的按键: {key_str}")
def __execute_keys(self, keys: List[Union[str, Key]]) -> None:
"""
Args:
keys:
"""
pressed_keys = []
try:
# 按下所有键
for key in keys:
self.keyboard.press(key)
pressed_keys.append(key)
# 释放所有键(按相反顺序)
for key in reversed(pressed_keys):
self.keyboard.release(key)
except Exception as e:
print(f"执行按键失败: {e}")
@router.websocket("/ws_wavecontrol")
@ -137,6 +199,9 @@ async def _handle_message(
elif message.type == WebSocketMessageType.MOUSE_CLICK:
gesture_handler.click_mouse()
# 处理键盘操作
elif message.type == WebSocketMessageType.SEND_KEYS:
gesture_handler.send_keys(data["key_str"])
except json.JSONDecodeError:
print("无效的JSON数据")
except Exception as e:

+ 29
- 0
wavecontrol-test/src/hand_landmark/gesture_handler.ts View File

@ -95,6 +95,13 @@ export class TriggerAction {
type: WsDataType.MOUSE_CLICK,
});
}
sendKeys(key_str: string) {
this.send({
type: WsDataType.SEND_KEYS,
data: { key_str },
});
}
}
// 手势处理
@ -199,6 +206,24 @@ export class GestureHandler {
}
/**
* -
*/
private handleFourFingers() {
try {
const key_str = this.app_store.config.four_fingers_up_send || "f";
const now = Date.now();
if (now - this.lastFullScreenTime < this.FULL_SCREEN_INTERVAL) {
return;
}
this.lastFullScreenTime = now;
this.triggerAction.sendKeys(key_str);
} catch (error) {
console.error("处理四指手势失败:", error);
}
}
/**
*
*/
handleGesture(gesture: HandGesture, hand: HandInfo) {
@ -223,6 +248,10 @@ export class GestureHandler {
this.handleMouseClick();
break;
case HandGesture.FOUR_FINGERS_UP:
this.handleFourFingers();
break;
}
}

+ 1
- 1
wavecontrol-test/src/store/app.ts View File

@ -18,7 +18,7 @@ export const use_app_store = defineStore("app-store", {
config: {
auto_start: false,
show_window: false,
four_fingers_up_send: "f",
four_fingers_up_send: "enter",
selected_camera_id: "",
// 识别框

Loading…
Cancel
Save