Skip to content

Instantly share code, notes, and snippets.

@sharunkumar
Last active March 28, 2024 10:06
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharunkumar/ca2cd57936e4bac974578d1e8a6b3cf6 to your computer and use it in GitHub Desktop.
Save sharunkumar/ca2cd57936e4bac974578d1e8a6b3cf6 to your computer and use it in GitHub Desktop.
Windows wrapper batch file for disabling keyboard and setting brightness before using scrcpy to connect. Settings are restores after scrcpy disconnects. Based on script by Volodymyr Shymanskyy. https://gist.github.com/vshymanskyy/a44ff7af2848653e91f269910cb9d50f
@echo off
rem Requires Null Keyboard https://play.google.com/store/apps/details?id=com.wparam.nullkeyboard
rem Author: Sharun Kumar
setlocal ENABLEDELAYEDEXPANSION
for /F %%i in ('adb shell settings get system screen_brightness') do set brightness=%%i
for /F %%i in ('adb shell settings get secure default_input_method') do set ime=%%i
for /F "tokens=1,2,3,4,5,6,7,8" %%G in ('adb shell media volume --get') do (
set vol=%%J
set volrange=%%M
)
for /F "tokens=1,2,3,4,5,6,7,8 delims=[.]" %%G in ("!volrange!") do (
set volmin=%%G
set volmax=%%H
)
call :Setup
call scrcpy
call :Restore
goto :EOF
:Setup
echo setting up
echo current brightness is !brightness!
echo current ime is !ime!
echo current volume is !vol! in range !volmin! to !volmax!
rem Wakeup to process commands faster
adb shell input keyevent KEYCODE_WAKEUP
rem Always-on screen
adb shell svc power stayon usb
rem Set brightness
adb shell settings put system screen_brightness_mode 0
adb shell settings put system screen_brightness 0
rem Disable Keyboard
adb shell ime set com.wparam.nullkeyboard/.NullKeyboard
rem Show Touches
adb shell settings put system show_touches 1
rem Set Max Volume
adb shell media volume --set !volmax!
goto :EOF
:Restore
echo restoring settings...
adb shell svc power stayon false
adb shell settings put system screen_brightness_mode 1
adb shell settings put system screen_brightness !brightness!
adb shell ime set !ime!
adb shell settings put system show_touches 0
adb shell media volume --set !vol!
goto :EOF
@CaTeNdrE
Copy link

Been using this for a long while but never thanked you. Thank you!

@onguarde
Copy link

onguarde commented Sep 2, 2021

adb shell ime set !ime!
this line doesn't seem to restore my keybaord back again, am i missing something?

I needed to do it explicitly like this instead,

adb shell ime set com.touchtype.swiftkey/com.touchtype.KeyboardService

I got the "com.touchtype.swiftkey/com.touchtype.KeyboardService", from the command prompt output of "echo current ime is !ime!", already in the script above.

@sharunkumar
Copy link
Author

adb shell ime set !ime! this line doesn't seem to restore my keybaord back again, am i missing something?

I needed to do it explicitly like this instead,

adb shell ime set com.touchtype.swiftkey/com.touchtype.KeyboardService

I got the "com.touchtype.swiftkey/com.touchtype.KeyboardService", from the command prompt output of "echo current ime is !ime!", already in the script above.

I am not completely sure as to why this doesn't work in the script for you. but if it works manually, then maybe some script above it is failing? try moving adb shell ime set !ime! just below the echo restoring settings... and see if it works?

@imasherrose
Copy link

i'm a bit late but i found out a reason why it might not restore back to the original keyboard. this MIGHT just be me and a coincidence but null keyboard shouldn't be running as your main keyboard the first time you launch scrcpy. i'm not sure why considering it doesn't save the intial keyboards information, i tested to make sure just now. but no. you just can't run null keyboard right away and it breaks the script i think? i might be wrong.. but pasting the script into my batch file again, saving it, changing the keyboard to my main keyboard, then running the batch file somehow fixed my issue.

@imasherrose
Copy link

also if anyone wants to know how to add scrcpy commands to your batch file alongside this script, i found that writing your scrcpy commands right next to "call scrcpy" (the one without anything next to it) makes it to where the script actually works and still keeps your scrcpy settings. i'm not sure if this is meant to be how it works, i'm not a coder. but it worked!

@sharunkumar
Copy link
Author

null keyboard shouldn't be running as your main keyboard the first time you launch scrcpy

yeah, when I wrote the script, my main keyboard would just be a keyboard other than the null keyboard, and it would save that info before setting it to null keyboard.

honestly though it's been a while since I've used this script myself hah. but nice to know that people are still using it 😁

@sharunkumar
Copy link
Author

writing your scrcpy commands right next to "call scrcpy"

if you mean command line parameters which you want to pass to scrcpy, yes, that's where you would put them. call scrcpy is the line where the actual binary is called, and when the executable closes, the restore command block is called, restoring all the settings

@sharunkumar
Copy link
Author

honestly though if I were to use the script now, I would just re-write it to PowerShell, which is a lot more readable and maintainable

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