Skip to content

Instantly share code, notes, and snippets.

@mlagerberg
Last active April 26, 2018 17:47
Show Gist options
  • Save mlagerberg/b0a20e6ccd5f6277401f to your computer and use it in GitHub Desktop.
Save mlagerberg/b0a20e6ccd5f6277401f to your computer and use it in GitHub Desktop.
[Android text input from your PC] #android #adb
@echo off
cls
echo.
echo Enter text and hit enter to send text
echo input to your phone or emulator.
echo Note: having adb in your path is required.
echo.
echo Type /help for a list of available commands,
echo /quit or Ctrl+C to quit.
echo.
setlocal enabledelayedexpansion
set space=%%s
:loop
set /p str="> " %=%
rem replace space with %s
set str=%str: =!space!%
rem replace " with \"
set str=%str:"=\"%
rem http://stackoverflow.com/questions/7789826/adb-shell-input-events
IF "%str%"=="/menu" (
adb shell input keyevent 1
) ELSE IF "%str%"=="/home" (
adb shell input keyevent 3
) ELSE IF "%str%"=="/back" (
adb shell input keyevent 4
) ELSE IF "%str%"=="/end_call" (
adb shell input keyevent 6
) ELSE IF "%str%"=="/up" (
adb shell input keyevent 19
) ELSE IF "%str%"=="/down" (
adb shell input keyevent 20
) ELSE IF "%str%"=="/left" (
adb shell input keyevent 21
) ELSE IF "%str%"=="/right" (
adb shell input keyevent 22
) ELSE IF "%str%"=="/center" (
adb shell input keyevent 23
) ELSE IF "%str%"=="/volume_up" (
adb shell input keyevent 24
) ELSE IF "%str%"=="/volume_down" (
adb shell input keyevent 25
) ELSE IF "%str%"=="/power" (
adb shell input keyevent 26
) ELSE IF "%str%"=="/camera" (
adb shell input keyevent 27
) ELSE IF "%str%"=="/search" (
adb shell input keyevent 84
) ELSE IF "%str%"=="/" (
adb shell input keyevent 66
) ELSE IF "%str%"=="/enter" (
adb shell input keyevent 66
) ELSE IF "%str%"=="/quit" (
goto quit
) ELSE IF "%str%"=="/help" (
echo Available commands:
echo - Emulate hardware buttons:
echo /menu
echo /home
echo /back
echo /power
echo /camera
echo /search
echo /volume_up
echo /volume_down
echo - D-pad buttons
echo /up
echo /down
echo /left
echo /right
echo /center
echo - Misc.
echo /end_call - Hang up
echo /enter or / - Action button, e.g. newline, next, ok, etc.
echo /quit - Exit this program
echo /help - Show this list of commands
) ELSE (
if "%str:~-1%" neq "/" (
adb shell input text '%str%'
adb shell input keyevent 66
) ELSE (
set str=%str:~0,-1%
adb shell input text '%str%'
)
)
goto loop
:quit
@echo off
cls
echo.
echo Enter text and hit enter to send text
echo input to your phone or emulator.
echo Press Ctrl+C at any time to quit.
echo Note: having adb in your path is required.
echo.
setlocal enabledelayedexpansion
set space=%%s
:loop
set /p str="> " %=%
rem replace space with %s
set str=%str: =!space!%
rem replace " with \"
set str=%str:"=\"%
adb shell input text '%str%'
adb shell input keyevent 66
goto loop
@mlagerberg
Copy link
Author

Useful for testing logins, entering mobile-keyboard-unfriendly passwords or sending texts/Whatsapps/Hangouts/etc.

  1. install adb (http://www.howtogeek.com/125769/how-to-install-and-use-abd-the-android-debug-bridge-utility/)
  2. connect phone to PC
  3. run the batch file
  4. tap a text field on your phone to give it focus
  5. type on your PC to enter text on your phone

Only for Windows PC (any version that runs adb) + Android phone. Does not work with Yo.

@mlagerberg
Copy link
Author

I've added an extended version of the bat file that offers a bunch of commands. It adds shortcuts for a bunch of hardware buttons and a command to quit. I'm keeping the shorter version around too, for its simplicity.

Although you can probably easily read this from the source, here's a list of the hardware buttons you can 'press' by typing their name with a / in front:

  • menu
  • home
  • back
  • power
  • camera
  • search
  • volume up (/volume_up)
  • volume down (/volume_down)
  • up
  • down
  • left
  • right
  • center
  • end call (/end_call)
  • enter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment