SwitchDefaultKeyboard.bat 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. @echo off
  2. chcp 65001 >nul
  3. setlocal enabledelayedexpansion
  4. echo ========================================
  5. echo Switch to Sogou Input Method
  6. echo ========================================
  7. echo.
  8. REM Get the directory where this batch file is located
  9. set "SCRIPT_DIR=%~dp0"
  10. set "PROJECT_ROOT=%SCRIPT_DIR%.."
  11. REM Use ADB from project root adb-tools directory
  12. set "ADB_PATH=%PROJECT_ROOT%\adb-tools\adb.exe"
  13. REM Check if local ADB exists, otherwise fallback to system PATH
  14. if not exist "%ADB_PATH%" (
  15. echo Warning: Local ADB not found at %ADB_PATH%
  16. echo Falling back to system PATH...
  17. set "ADB_PATH=adb"
  18. )
  19. echo [0/5] Using ADB: %ADB_PATH%
  20. echo.
  21. REM Set fixed device address
  22. set "DEVICE_ADDRESS=192.168.2.5:5555"
  23. set "USE_DEVICE_OPTION=-s !DEVICE_ADDRESS!"
  24. echo Using device: !DEVICE_ADDRESS!
  25. echo.
  26. echo [1/5] Check if ADB is available...
  27. "%ADB_PATH%" version >nul 2>&1
  28. if errorlevel 1 (
  29. echo Error: ADB not found at: %ADB_PATH%
  30. echo Please ensure adb.exe exists in %PROJECT_ROOT%\adb-tools\ directory
  31. pause
  32. exit /b 1
  33. )
  34. echo ADB is available
  35. echo.
  36. echo [2/5] Check connected devices...
  37. echo.
  38. "%ADB_PATH%" devices
  39. echo.
  40. REM Try to connect if not already connected
  41. echo Checking if device !DEVICE_ADDRESS! is connected...
  42. "%ADB_PATH%" !USE_DEVICE_OPTION! get-state >nul 2>&1
  43. if errorlevel 1 (
  44. echo Device not connected, attempting to connect...
  45. "%ADB_PATH%" connect !DEVICE_ADDRESS! >nul 2>&1
  46. timeout /t 2 /nobreak >nul
  47. "%ADB_PATH%" !USE_DEVICE_OPTION! get-state >nul 2>&1
  48. if errorlevel 1 (
  49. echo Error: Cannot connect to device !DEVICE_ADDRESS!
  50. echo.
  51. echo Please ensure:
  52. echo 1. Device is on the same network
  53. echo 2. Run 0-TEST-CONNECT.BAT to test connection first
  54. pause
  55. exit /b 1
  56. )
  57. echo Device connected successfully
  58. ) else (
  59. echo Device already connected
  60. )
  61. echo.
  62. echo.
  63. echo [3/5] Check current default input method...
  64. echo.
  65. for /f "delims=" %%i in ('"%ADB_PATH%" !USE_DEVICE_OPTION! shell settings get secure default_input_method') do set "CURRENT_IME=%%i"
  66. set "CURRENT_IME=!CURRENT_IME: =!"
  67. echo Current default IME: !CURRENT_IME!
  68. echo.
  69. REM Target input method: Sogou Input Method
  70. set "TARGET_IME=com.sohu.inputmethod.sogou/.SogouIME"
  71. REM Check if already using target input method
  72. if "!CURRENT_IME!"=="!TARGET_IME!" (
  73. echo Sogou Input Method is already the default input method.
  74. echo No action needed.
  75. pause
  76. exit /b 0
  77. )
  78. echo [4/5] List available input methods...
  79. echo.
  80. "%ADB_PATH%" !USE_DEVICE_OPTION! shell ime list -s
  81. echo.
  82. echo [5/5] Switch to Sogou Input Method...
  83. echo Setting !TARGET_IME! as default input method...
  84. "%ADB_PATH%" !USE_DEVICE_OPTION! shell ime enable !TARGET_IME!
  85. if errorlevel 1 (
  86. echo Warning: Failed to enable Sogou Input Method
  87. echo Make sure Sogou Input Method is installed on your device
  88. echo.
  89. ) else (
  90. echo Sogou Input Method enabled successfully
  91. echo.
  92. )
  93. "%ADB_PATH%" !USE_DEVICE_OPTION! shell ime set !TARGET_IME!
  94. if errorlevel 1 (
  95. echo Error: Failed to set default input method
  96. pause
  97. exit /b 1
  98. )
  99. echo Default input method set successfully
  100. echo.
  101. echo Verifying current default input method...
  102. for /f "delims=" %%i in ('"%ADB_PATH%" !USE_DEVICE_OPTION! shell settings get secure default_input_method') do set "NEW_IME=%%i"
  103. set "NEW_IME=!NEW_IME: =!"
  104. echo Current default IME: !NEW_IME!
  105. echo.
  106. if "!NEW_IME!"=="!TARGET_IME!" (
  107. echo ========================================
  108. echo Success: Sogou Input Method is now default
  109. echo ========================================
  110. ) else (
  111. echo ========================================
  112. echo Warning: Failed to set Sogou Input Method as default
  113. echo Expected: !TARGET_IME!
  114. echo Current: !NEW_IME!
  115. echo ========================================
  116. )
  117. echo.
  118. pause