Clear Edit Text - adb
Asked Answered
G

5

10

How to clear focused Edit text using shell command.

I tried

adb shell input keyevent KEYCODE_CLEAR // Not worked 
adb shell input keyevent KEYCODE_DEL // Delete only one char
adb shell input keyevent KEYCODE_FORWARD_DEL // Not worked

With this I am only able to delete upto One character only, Is there any way I can delete/clear the focused Edit text.

Graig answered 7/9, 2015 at 7:31 Comment(3)
try using adb shell input text '' Ariannaarianne
Invalid arguments for command: text, Even If I make it run anyhow it will add new space in already input text.Graig
I am unable to find any such command which can clear the whole edittext's data.Ariannaarianne
G
17

This works for me:

function clear_input() {
    adb shell input keyevent KEYCODE_MOVE_END
    adb shell input keyevent --longpress $(printf 'KEYCODE_DEL %.0s' {1..250})
}

Then:

clear_input
Garland answered 6/4, 2016 at 13:54 Comment(6)
What are KEYCODE_MOVE_END and KEYCODE_DEL?Mephitis
KEYCODE_MOVE_END = move the cursor to the end of lineGarland
KEYCODE_DEL = delete the character in the current cursor positionGarland
I think that is not obvious and that is correct syntax because it is a constant and not an environment variable: developer.android.com/reference/android/view/KeyEventGarland
I already upvoted and used your answer, but I'm just commenting to improve it. That is incorrect Bash syntax, period. Constants/variables in Bash are declared without $ but to get their value you need to prefix them with $. But hey, be my guest and avoid learning something ;)Mephitis
Man, those values are constants of Android SDK, not BASH. Please, take a look at the link this time. developer.android.com/reference/android/view/…Garland
E
6

This will select all and then backspace.

input keycombination 113 29 && input keyevent 67
Enchanter answered 28/2, 2023 at 7:37 Comment(5)
Amazing - this should be the top-rated solution!Ironhanded
it works for emulator, but when I check it with Samsung mobile, I get this error Error: Unknown command: keycombination Usage: input [<source>] <command> [<arg>...] The sources are: dpad keyboard mouse touchpad gamepad touchnavigation joystick touchscreen stylus trackballMatsu
Rasoul Miri ; just tried a Samsung Galaxy Z Fold 2 here and the command works ok. Try using "" to avoid problems ?; C:\WINDOWS\system32>adb shell "input keycombination 113 29 && input text "+15551212"Marc
This is the best solution. I use it with slight improvement to overwrite the previous content with new value in one line like this : >adb shell "input keycombination 113 29 && input text "new text"Marc
Not working for me, API 27 emulator returns Error: Unknown command: keycombinationStickle
M
4

You can use this way:

adb shell input keyevent --longpress 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67

67 is the keycode of KEYCODE_DEL

Matsu answered 10/5, 2022 at 12:12 Comment(2)
Need to go to the end of the edit text, thenCircuity
First, go to end of string with "adb shell input keyevent --longpress KEYCODE_MOVE_END" Takes a long time; low end devices could be a few 100 msec per caracter. Some implementation will require to add a wait after this.Marc
S
2

If you use uiautomator(https://github.com/xiaocong/uiautomator), you could do this by:

  1. tap the EditText widget to get focus, then

  2. use device(focused=True).clear_text() to clear the view, or by device(focused=True).set_text("new text") to set a new text.

Spritsail answered 11/12, 2015 at 2:39 Comment(1)
Question is about ADBMephitis
R
1

The only way that I have found so far is to get the proper coordinates to use input swipe x y x y duration to simulate a long press. This will then highlight all the text in the EditText field. You can then send the keys you want to replace what was there.

I just wish that adb shell input keyevent KEYCODE_CLEAR would clear all the text in the field. That would make things so much easier, if someone can find a better way that would be great.

Robbins answered 27/11, 2015 at 17:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.