Skip to content

Commit 1e7195f

Browse files
authored
fix send_keys, use ime when pasteClipboard failed (#1039)
1 parent 9f0a946 commit 1e7195f

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

uiautomator2/__init__.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,12 @@ def set_clipboard(self, text, label=None):
587587

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

592597
def send_keys(self, text: str, clear: bool = False):
593598
"""
@@ -599,8 +604,14 @@ def send_keys(self, text: str, clear: bool = False):
599604
"""
600605
if clear:
601606
self.clear_text()
602-
self.clipboard = text
603-
self.jsonrpc.pasteClipboard()
607+
try:
608+
# setClipboard的兼容性并不是特别好,但是这样做有个优点就是不用装输入法了。
609+
self.clipboard = text
610+
if self.clipboard != text:
611+
raise UiAutomationError("setClipboard failed")
612+
self.jsonrpc.pasteClipboard()
613+
except:
614+
self._send_keys_with_ime(text)
604615

605616
def keyevent(self, v):
606617
"""

uiautomator2/_input.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ def _must_broadcast(self, action: str, extras: Dict[str, str] = {}):
9696
if result.code != BORADCAST_RESULT_OK:
9797
raise AdbBroadcastError(f"broadcast {action} failed: {result.data}")
9898

99-
@deprecated(reason="use send_keys instead")
10099
def _send_keys_with_ime(self, text: str):
101100
try:
102101
self.set_input_ime()
@@ -140,7 +139,6 @@ def send_action(self, code: Union[str, int] = None):
140139
else:
141140
self._must_broadcast('ADB_KEYBOARD_SMART_ENTER')
142141

143-
@deprecated(reason="use clear_text() instead")
144142
def _clear_text_with_ime(self):
145143
""" clear text
146144
Raises:

0 commit comments

Comments
 (0)