mouse and keyboard controllers for windows and linux

This commit is contained in:
Jing Hua
2023-11-08 09:22:43 +08:00
parent 8bc4459f41
commit a8aebf5d15
9 changed files with 271 additions and 48 deletions

View File

@@ -1,9 +1,10 @@
from abc import ABC, abstractmethod
from fabric import Connection
from xdotool import XDoToolController
from .xdotool import XDoToolController
from .python import PythonController
class AbstractMouseController(ABC):
class AbstractKeyboardController(ABC):
@abstractmethod
def type(self, text: str):
raise NotImplementedError
@@ -12,7 +13,7 @@ class AbstractMouseController(ABC):
def key(self, key: str):
raise NotImplementedError
class XDoToolKeyboardController(AbstractMouseController, XDoToolController):
class XDoToolKeyboardController(AbstractKeyboardController, XDoToolController):
def __init__(self, ssh_connection: Connection):
super().__init__(ssh_connection=ssh_connection)
@@ -22,16 +23,13 @@ class XDoToolKeyboardController(AbstractMouseController, XDoToolController):
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()
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(f"write({text})")
self._execute_python_command(self.command.format(command=f"keyboard.write('{text}')"))
def key(self, key: str):
self._execute_python_command(f"press_and_release({key})")
self._execute_python_command(self.command.format(command=f"keyboard.press_and_release('{key}')"))