From 17cd6653081d6e22335a6468682064c963164fe8 Mon Sep 17 00:00:00 2001 From: codeskyblue Date: Wed, 22 May 2024 16:03:40 +0800 Subject: [PATCH] add long_press, close #889 --- uiautomator2/__init__.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/uiautomator2/__init__.py b/uiautomator2/__init__.py index dd64d81..dcea9a4 100644 --- a/uiautomator2/__init__.py +++ b/uiautomator2/__init__.py @@ -486,6 +486,23 @@ def press(self, key: Union[int, str], meta=None): key, meta) if meta else self.jsonrpc.pressKeyCode(key) else: return self.jsonrpc.pressKey(key) + + def long_press(self, key: Union[int, str]): + """ + long press key via name or key code + + Args: + key: key name or key code + + Examples: + long_press("home") same as "adb shell input keyevent --longpress KEYCODE_HOME" + """ + with self._operation_delay("press"): + if isinstance(key, int): + self.shell("input keyevent --longpress %d" % key) + else: + key = key.upper() + self.shell(f"input keyevent --longpress {key}") def screen_on(self): self.jsonrpc.wakeUp()