Skip to content

Commit

Permalink
fix send_keys, use ime when pasteClipboard failed (#1039)
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue authored Sep 24, 2024
1 parent 9f0a946 commit 1e7195f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 14 additions & 3 deletions uiautomator2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,12 @@ def set_clipboard(self, text, label=None):

def clear_text(self):
""" clear input text """
self.jsonrpc.clearInputText()
try:
# 这个问题基本不大
# 不过考虑到u2.jar不一定升级成功了,所以还有留个兜底方案·
self.jsonrpc.clearInputText()
except:
self._clear_text_with_ime()

def send_keys(self, text: str, clear: bool = False):
"""
Expand All @@ -599,8 +604,14 @@ def send_keys(self, text: str, clear: bool = False):
"""
if clear:
self.clear_text()
self.clipboard = text
self.jsonrpc.pasteClipboard()
try:
# setClipboard的兼容性并不是特别好,但是这样做有个优点就是不用装输入法了。
self.clipboard = text
if self.clipboard != text:
raise UiAutomationError("setClipboard failed")
self.jsonrpc.pasteClipboard()
except:
self._send_keys_with_ime(text)

def keyevent(self, v):
"""
Expand Down
2 changes: 0 additions & 2 deletions uiautomator2/_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ def _must_broadcast(self, action: str, extras: Dict[str, str] = {}):
if result.code != BORADCAST_RESULT_OK:
raise AdbBroadcastError(f"broadcast {action} failed: {result.data}")

@deprecated(reason="use send_keys instead")
def _send_keys_with_ime(self, text: str):
try:
self.set_input_ime()
Expand Down Expand Up @@ -140,7 +139,6 @@ def send_action(self, code: Union[str, int] = None):
else:
self._must_broadcast('ADB_KEYBOARD_SMART_ENTER')

@deprecated(reason="use clear_text() instead")
def _clear_text_with_ime(self):
""" clear text
Raises:
Expand Down

0 comments on commit 1e7195f

Please sign in to comment.