DEV Community

Cover image for Toggling laptop touchscreen with a keyboard shortcut
Shiraz
Shiraz

Posted on • Originally published at github.com

Toggling laptop touchscreen with a keyboard shortcut

My college friends had a fascination with dragging their fingers across my laptop touchscreen. I wasn't so keen, and thus Touchscreen Toggle (TST) was created, originally a script which toggled my touchscreen with a keyboard shortcut.

This project was my first experience working with device drivers, and uses the Windows devcon utility to disable and re-enable a device driver.

I first used devcon to determine which Human Interface Device was responsible for my touchscreen, which in my case was HID\VEN_8086&DEV_9D3E&SUBSYS_00000000&REV_21&COL05*. Using devcon disable on this HID disabled my screens touch functionality, and using devcon enable restored the touch functionality.

Toggle script

I created a small batch script which would toggle the driver based on the output from devcon status.

  1. If the string Driver is running. was found, it would disable the driver.
  2. If it instead failed to find that string, it would enable the driver.
devcon status "HID\VEN_8086&DEV_9D3E&SUBSYS_00000000&REV_21&COL05*" | find /i "Driver is running.">nul
if not %errorlevel%==1 (
    devcon disable "HID\VEN_8086&DEV_9D3E&SUBSYS_00000000&REV_21&COL05*"
) else (
    devcon enable "HID\VEN_8086&DEV_9D3E&SUBSYS_00000000&REV_21&COL05*"
)
Enter fullscreen mode Exit fullscreen mode

I saved the driver toggle script in C:\Windows\TST\TST.bat.

Keyboard shortcut

Having to run that script manually as an administrator was inconvenient and slow, by which time my friends would have reorganised my windows. I looked into running a script as an administrator with a keyboard shortcut and settled on using AutoHotkey.

Devcon required administrative privileges to run, so I set AutoHotkey to run and execute all scripts as administrator (Properties -> Compatibility -> Run this program as administrator).

I then created this AutoHotkey script to run it when I press the key combination Right Alt + F12.

RAlt & F12::Run, C:\Windows\TST\TST.bat
Enter fullscreen mode Exit fullscreen mode

I saved this AutoHotkey script as DriverToggle.ahk.

Toast notifications

It was working perfectly, and I was able to quickly disable my touchscreen to mitigate surprise inbound fingers from disrupting my workflow.

It became a guessing game knowing when my touch screen was enabled though, so to make my script even fancier I looked into generating Windows notifications from the command line.

I found an article showing how to achieve this in PowerShell using the BurntToast module.

My PowerShell ExecutionPolicy was set to Restricted which does not allow installing any remote modules, so I had to first change my ExecutionPolicy:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Enter fullscreen mode Exit fullscreen mode

Next, I ran Install-Module -Name BurntToast to install BurntToast.

Finally, I added my logo to C:\Windows\TST\image.png) and edited the driver toggle script to generate toast notifications with my logo as the icon using powershell New-BurntToastNotification:

devcon status "HID\VEN_8086&DEV_9D3E&SUBSYS_00000000&REV_21&COL05*" | find /i "Driver is running.">nul
if not %errorlevel%==1 (
    devcon disable "HID\VEN_8086&DEV_9D3E&SUBSYS_00000000&REV_21&COL05*"
        powershell New-BurntToastNotification -Text 'Touch Screen Toggled', 'Your touch screen is now disabled.' -AppLogo image.png
) else (
    devcon enable "HID\VEN_8086&DEV_9D3E&SUBSYS_00000000&REV_21&COL05*"
        powershell New-BurntToastNotification -Text 'Touch Screen Toggled', 'Your touch screen has been enabled.' -AppLogo image.png
)
Enter fullscreen mode Exit fullscreen mode

PowerShell script

I noticed that the toast notifications took a couple of seconds to appear, and instead of a black command prompt box appearing and closing immediately when running my driver toggle script, it would stay open till the toast notification appeared.

I also observed that running powershell New-BurntToastNotification from Command Prompt has the same delay, but running New-BurntToastNotification directly from PowerShell had no delay.

This led me to rewriting the driver toggle script in PowerShell:

if(devcon status "HID\VEN_8086&DEV_9D3E&SUBSYS_00000000&REV_21&COL05*" | find /i "Driver is running.") {
    if(devcon disable "HID\VEN_8086&DEV_9D3E&SUBSYS_00000000&REV_21&COL05*" | find /i "Disable failed") {
        New-BurntToastNotification -Text "Touch Screen Toggle Failed", "Failed to disable your touch screen (Am I running with administrator permissions? Is your hardware ID correct?)" -AppLogo image.png
    } else {
        New-BurntToastNotification -Text "Touch Screen Toggled", "Your touch screen is now disabled." -AppLogo image.png
    }
} else {
    if(devcon enable "HID\VEN_8086&DEV_9D3E&SUBSYS_00000000&REV_21&COL05*" | find /i "Enable failed") {
        New-BurntToastNotification -Text "Touch Screen Toggle Failed", "Failed to enable your touch screen (Am I running with administrator permissions? Is your hardware ID correct?)" -AppLogo image.png
    } else {
        New-BurntToastNotification -Text 'Touch Screen Toggled', "Your touch screen has been enabled." -AppLogo image.png
    }
}
Enter fullscreen mode Exit fullscreen mode

After changing my AutoHotkey script to point to this PowerShell script instead, I still observed the same delay. I concluded that the delay was PowerShell itself starting up, and my tests where PowerShell had no delay was due to PowerShell already having started up.

Touchpad Toggle!

After I did all of this with my touchscreen, I identified which HID device was behind my touchpad and wrote a script to toggle that too, because why not:

devcon status "HID\MSHW0092&COL02" | find /i "Driver is running.">nul
if not %errorlevel%==1 (
    devcon disable "HID\MSHW0092&COL02"
) else (
    devcon enable "HID\MSHW0092&COL02"
)
Enter fullscreen mode Exit fullscreen mode

This was saved to C:\Windows\TST\TPT.bat, and the following line was appended to DriverToggle.ahk:

RAlt & F11::Run, C:\Windows\TST\TPT.bat
Enter fullscreen mode Exit fullscreen mode

I decided not to do the same for my keyboard HID..

Conclusion

This was a very fun project to work on, and may be one of the most useful personal tools I have created. I've since moved to daily driving Linux, so I'm unable to check if these scripts still work, nor can I perform testing to remember specifics such as how exactly I went about finding the correct HID for my touchscreen.

The same touchscreen has unfortunately started experiencing phantom tapping so I've disabled it, and I won't be making a linux version of this solution because of that.

Vector Vectors by Vecteezy

Top comments (0)