Skip to content

Instantly share code, notes, and snippets.

@emabrey
Created August 26, 2019 23:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emabrey/f88357aa25c71720738d44aac58bdb7a to your computer and use it in GitHub Desktop.
Save emabrey/f88357aa25c71720738d44aac58bdb7a to your computer and use it in GitHub Desktop.
Toggle Touchscreen Support
@ECHO OFF
:: This file enables the touchscreen when disabled and disables the touchscreen when enabled
:: There is an annoying bug fixed by using AutoIt to unpress the Windows Key
:: Make sure devcon_x64.exe and autoit3_x64.exe are present for x64
:: devcon_x86.exe and autoit3.exe could be used for x86, but you need to replace
:: DEVCON_BINARY and AUTOIT_BINARY below
:: These binaries need to be placed in the location pointed to by DEVCON_DIR for the script
:: to find them
:: Create ECHO macro to santize output characters
SET "ECHO=FOR /F %%^" IN ("""") DO ECHO:%%~""
:: Setup the user and admin level commands for the devcon utility
SET "DEVCON_DIR=%userprofile%\.utilities\devcon"
SET "DEVCON_BINARY=devcon_x64.exe"
:: Setup the AutoIt utility location
SET "AUTOIT_BINARY=autoit3_x64.exe"
:: These are the values for my Lenovo Flex 5-1570 (Model 81CA)
:: You can find yours in Device Manager
SET "DEVICE_CLASS=HIDClass"
SET "DEVICE_ID=HID\VEN_WCOM*DEV_50E6*Col01"
:: Doesn't need elevation
SET "STATUS_CMD=%DEVCON_DIR%\%DEVCON_BINARY% status =%DEVICE_CLASS% %DEVICE_ID%"
::Does need elevation
SET "ENABLE_CMD=powershell -Command ^"Start-Process -WindowStyle Hidden -FilePath '%DEVCON_BINARY%' -ArgumentList 'enable =%DEVICE_CLASS% %DEVICE_ID%' -WorkingDirectory '%DEVCON_DIR%' -Verb runAs^""
SET "DISABLE_CMD=powershell -Command ^"Start-Process -WindowStyle Hidden -FilePath '%DEVCON_BINARY%' -ArgumentList 'disable =%DEVICE_CLASS% %DEVICE_ID%' -WorkingDirectory '%DEVCON_DIR%' -Verb runAs^""
:: Generate a variable for each line of the status cmd output. E.g. status_line_2 is the 2nd line of output
SETLOCAL ENABLEDELAYEDEXPANSION
SET "count=1"
FOR /F "tokens=* USEBACKQ" %%F IN (`CALL %STATUS_CMD%`) DO (
SET "status_line_!count!=%%F"
SET /a "count=!count!+1"
)
SETLOCAL DISABLEDELAYEDEXPANSION
::%ECHO%%status_line_1%
::%ECHO%%status_line_2%
::%ECHO%%status_line_3%
::ECHO:%STATUS_CMD%
::ECHO:%ENABLE_CMD%
::ECHO:%DISABLE_CMD%
:: Toggle the device
IF /I "%status_line_3%" == "Driver is running." (
%ECHO%Device is enabled: disabling device.
%DISABLE_CMD%
) ELSE (
IF /I "%status_line_3%" == "Device is disabled." (
%ECHO%Device is disabled: enabling device.
%ENABLE_CMD%
)
)
SET 'WINDOWS_KEY_CMD=Send("{LWINDOWN}{LWINUP}{RWINDOWN}{RWINUP}")'
:: Use AutoIt to virtually unpress the windows key to stop stupid bug
SET "AUTOIT_CMD=powershell -Command ^"Start-Process -WindowStyle Hidden -FilePath '%AUTOIT_BINARY%' -ArgumentList '/AutoIt3ExecuteLine %WINDOWS_KEY_CMD%'^""
%AUTOIT_CMD%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment