Files
sci-gui-agent-benchmark/desktop_env/controllers/keyboard.py
2023-11-02 11:10:55 +08:00

37 lines
1.2 KiB
Python

from abc import ABC, abstractmethod
from fabric import Connection
from xdotool import XDoToolController
class AbstractMouseController(ABC):
@abstractmethod
def type(self, text: str):
raise NotImplementedError
@abstractmethod
def key(self, key: str):
raise NotImplementedError
class XDoToolKeyboardController(AbstractMouseController, XDoToolController):
def __init__(self, ssh_connection: Connection):
super().__init__(ssh_connection=ssh_connection)
def type(self, text: str):
self._execute_xdotool_command(f"type {text}")
def key(self, key: str):
self._execute_xdotool_command(f"key {key}")
class PythonKeyboardController(AbstractMouseController):
def __init__(self, ssh_connection: Connection):
self.ssh_connection = ssh_connection
def _execute_python_command(self, command: list[str]) -> None:
result = self.ssh_connection.run(f"sudo python3 -c 'import keyboard; keyboard.{command}'", hide=True)
return result.stdout.strip()
def type(self, text: str):
self._execute_python_command(f"write({text})")
def key(self, key: str):
self._execute_python_command(f"press_and_release({key})")