Files
sci-gui-agent-benchmark/desktop_env/controllers/keyboard.py
2023-11-26 23:54:43 +08:00

57 lines
1.8 KiB
Python

from abc import ABC, abstractmethod
from fabric import Connection
from .xdotool import XDoToolController
from .python import PythonController
class AbstractKeyboardController(ABC):
@abstractmethod
def type(self, text: str):
raise NotImplementedError
@abstractmethod
def key(self, key: str):
raise NotImplementedError
@abstractmethod
def key_down(self, key: str):
raise NotImplementedError
@abstractmethod
def key_up(self, key: str):
raise NotImplementedError
class XDoToolKeyboardController(AbstractKeyboardController, 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}")
def key_down(self, key: str):
self._execute_xdotool_command(f"keydown {key}")
def key_up(self, key: str):
self._execute_xdotool_command(f"keyup {key}")
class PythonKeyboardController(AbstractKeyboardController, PythonController):
def __init__(self, http_server: str):
super().__init__(http_server=http_server)
self.command = "python -c \"import keyboard; {command}\""
def type(self, text: str):
self._execute_python_command(self.command.format(command=f"keyboard.write('{text}')"))
def key(self, key: str):
self._execute_python_command(self.command.format(command=f"keyboard.press_and_release('{key}')"))
def key_down(self, key: str):
self._execute_python_command(self.command.format(command=f"keyboard.press('{key}')"))
def key_up(self, key: str):
self._execute_python_command(self.command.format(command=f"keyboard.release('{key}')"))