Design a hot key in Windows to Toggle the visibility of Desktop icons

Design a hot key in Windows to Toggle the visibility of Desktop icons

I like my Desktop screen to be organized and simplistic.

( Check out https://github.com/prasanthrangan/hyprdots if you like the above Desktops )

I love my wallpapers, and often when I am done with my work, I just stare at my wallpaper for some time and be lost in thoughts. But I there was a time when my desktop had this pesky thing called icons. Even though it was organized, icons always were always obstructive to my view of Wallpaper.

Now that I baited you all to think that was a Windows desktop ( No it's linux, and this is your cue to start using Linux ) I feel bad. But I can make it up to you by introducing a really awesome world to you - AHK ( The best tool to automate stuff in Windows, even better than Powershell and Python ) though only known to crackhead gamers and hobby programmers. And using this we will solve the problem of obstructive icons.

Solution ? Code out a script to toggle the visibility of icons in the desktop !

So I have a script written in AutoHotKey language - something that is one of my favourite utilities in Windows. AutoHotkey is a powerful scripting language for Windows. It offers a wide variety of functionalities like :-

  • automate repetitive tasks

    • text expansion

    • launching programs

    • controlling mouse movements and clicks

  • create custom keyboard shortcuts

  • manipulate windows and files with ease

  • enhancing gaming experiences with custom macros

It provides a simple syntax and a wide range of built-in functions for automating various tasks, making it a popular choice for both beginners and experienced users alike.

Install AutoHotKey

There is an AHK version 1, and a newer and better version of AHK which is now available. But for the purposes of this article we'll be using the version 1 only.

Download and install AutoHotkey from the official website: https://www.autohotkey.com/

Write a script

;Press F12 to hide or unhide desktop icons
F12::
ControlGet, HWND, Hwnd, , SysListView321, ahk_class Progman
If HWND =
    ControlGet, HWND, Hwnd, , SysListView321, ahk_class WorkerW
    If DllCall("IsWindowVisible", UInt, HWND)
        WinHide, ahk_id%HWND%
    Else
        WinShow, ahk_id%HWND%

This script is what the magic portion is. Here's a breakdown of how it works:

  1. ^+!q:: :-

    • ^ represents the Ctrl key.

    • + represents the Shift key.

    • ! represents the Alt key.

    • q represents the letter Q.

  2. ControlGet :-

    • used to retrieve information about a control within a window.

    • Controls are graphical elements within a window, such as buttons, text boxes, list views, etc.

  3. ControlGet, HWND, Hwnd, , SysListView321, ahk_class Progman :-

    • This command retrieves the handle (HWND) of the desktop's list view control, which is RESPONSIBLE FOR DISPLAYING ICONS.

    • It searches for the control belonging to the Progman class, which is the desktop window.

  4. If HWND = :-

    • Typical condition statement

    • Checks if the retrieved handle (HWND) is empty.

    • If it is empty, it means that the script failed to find the desktop icons using the Progman class

  5. ControlGet, HWND, Hwnd, , SysListView321, ahk_class WorkerW :-

    • If the Progman class search fails, this command searches for the desktop icons using the WorkerW class.
  6. If DllCall("IsWindowVisible", UInt, HWND) :-

    • This line uses a DLL call

      • Invoking a function or method from a dynamic link library, which is a collection of code and data that can be used by multiple programs simultaneously.

      • DLLs enables AHK scripts to access functions exposed by it to perform various REALLY USEFUL tasks, such as

        • interacting with the operating system ( which is what we are doing here )

        • accessing hardware devices

        • performing complex computations, ect

    • The DLL call is to check if the desktop icons window a.k.a desktop (identified by its handle HWND) is currently visible.

  7. The logic we were waiting for :-

    WinHide, ahk_id%HWND% :-

    • If the desktop icons is visible, this command hides it.

Else WinShow, ahk_id%HWND%: :-

  • If the desktop icons is hidden, this command shows it.

Basically all that is happening is :-

  • Ctrl + Shift + Alt + Q to toggle visibility of the icons

  • It first attempts to find the icons using the Progman class

  • then falls back to the WorkerW class if necessary.

  • Depending on the current visibility state of the desktop icons, it either hides or shows them.

That's it !!

Make it run automatically on start-up

To add a script to startup in Windows 10, you can follow these steps:

  1. Open the Startup Folder:

    • Press Win + R to open the Run dialog.

    • Type shell:startup and press Enter. This will open the Startup folder.

  2. Copy Your Script:

    • Copy the script file (with .ahk extension) that you want to run at startup.
  3. Paste Into Startup Folder:

    • Right-click inside the Startup folder and select "Paste" to paste the script file into the folder.
  4. Verify:

    • Double-click the script file in the Startup folder to ensure it runs as expected. You should see your script executing.
  5. Restart Your Computer:

    • Restart your computer to test if the script runs automatically after startup.

After following these steps, your script should run automatically every time you start up your computer.

More on AutoHotKey

This is only a fraction of the capacilities that AHK equips us with.

  • You can make scripts to help you perform better in games ( they have a channel dedicated to that in their discord, check them out ! )

  • make hotkeys to open commonly used directories in file manager

  • something called as hotstrings that can replace a user-defined strings with another user-defined string

    • I've mhade it so that if I type nmn, it gets replaced my name SYSTEM-WIDE !

    • it's super useful when I take math notes. If I type iid, it'll be replaced by identically and independentlly distributed

    • Writing LaTeX codes for all notes, assignments can be automated too.

  • Have a workflow and have things to open on startup ( which a dude of mine made a script in Python, check out here ) but in AHK it's this easy !

    •     Run "C:\Path\to\Obsidian.exe"
          Sleep 1000  
          Run "C:\Path\to\chrome.exe"
          Sleep 1000  
          Run "C:\Path\to\Code.exe"
          Sleep 1000
      

This might be a good place to see what people have been able to do with AHK.

https://www.reddit.com/r/AutoHotkey/