📅 August 17, 2021

AutoHotkey

AutoHotkey is a fantastic program for reducing the amount of time it takes you to perform Windows tasks.

I previously wrote about how to change the Windows volume by using your mouse scroll wheel while hovering over the taskbar. That is one of my most used AutoHotkey scripts, but I use several more daily that I wanted to share. Most of these scripts I found from various sites with some modifications sprinkled in.

Open Bluetooth Devices

I open up the Bluetooth settings on my Windows laptop frequently because I switch between wireless headphones. This provides a quick way to open that up.

;--------------------------------------------------------------------
; Open Bluetooth devices
;--------------------------------------------------------------------
^+B::
Run, bthprops.cpl
return

Open Windows Terminal

Programming requires me to open a terminal often. This script allows me to open a Windows Terminal quickly. It will open Windows Terminal at the current path if I'm browsing a directory. Otherwise, it will set the current path to my desktop.

;--------------------------------------------------------------------
; Open Windows Terminal in the current directory or Desktop
;--------------------------------------------------------------------
^+w::
if explorerHwnd := WinActive("ahk_class CabinetWClass")
{
	for window in ComObjCreate("Shell.Application").Windows
	{
		if (window.hwnd = explorerHwnd)
			CurrentExplorerPath := window.Document.Folder.Self.Path
	}
	run, "wt.exe", %CurrentExplorerPath%
}
else
{
	run, "wt.exe", C:\Users\YourUsernameHere\Desktop
}

Close / Minimize Windows

The Caps Lock key is something I don't use frequently. Something I do frequently is close and minimize windows. This script allows the Caps Lock key to close the active window if it is pressed, or minimize the active window if pressed longer than .2 seconds and released. There is also some extra logic for specific programs (Chrome, VS Code, Visual Studio) to only close the active tab rather than closing the entire program.

;--------------------------------------------------------------------
; Close (hit Capslock) / Minimize (hold Capslock & release) active window
;--------------------------------------------------------------------

$Capslock::

; Wait no more than 'n' seconds for key release (also suppress auto-repeat)
KeyWait, Capslock, T0.2

; timeout, so key is still down...
if ErrorLevel
{
	KeyWait, Capslock, T1.5

	; No timeout, so key was released
	if !ErrorLevel
	{
		WinMinimize, A
		return
	}

	; Wait for button to be released
	WinMinimize, A
	return
}
else
{
	if (WinActive("ahk_exe chrome.exe") || WinActive("ahk_exe code.exe") || WinActive("ahk_exe devenv.exe"))
	{
		Send ^w
		return
	}
	else
	{
		WinClose, A
		return
	}
}

Media

I'm usually listening to music while I work, and my current keyboard doesn't have media keys (play/stop/next/volume up/down). These instructions add the ability to control Spotify, YouTube, etc.

;--------------------------------------------------------------------
; Media Hotkeys
;--------------------------------------------------------------------

^+Alt::Send {Media_Play_Pause}
^+Space::Send {Media_Next}
^+PgUp::Send {Volume_Up}
^+PgDn::Send {Volume_Down}

Toggle mic mute for Microsoft Teams

Teams has a keyboard shortcut for toggling mic mute, but Teams needs to be the active window for this work. This script allows for the toggling mic mute in Teams, even when Teams is not the active window.

;--------------------------------------------------------------------
; Toggle mic mute for Microsoft Teams
;--------------------------------------------------------------------

Pause::
; Get IDs for all teams windows
WinGet, id, list, ahk_exe Teams.exe

; Loop through IDs of all teams windows
Loop, %id%
{
	this_ID := id%A_Index%

	; Get the title of the current window
	WinGetTitle, Title, ahk_id %this_ID%

	; Make sure title is not the notification
	if Title <> Microsoft Teams Notification
	{
		; Screen sharing win uses null title, make sure the win does not have a null title
		if Title <>
		{
			; This should be the correct win, activate it
			WinActivate, ahk_id %this_ID%

			; Send Ctrl Shift M shortcut
			Send, ^M

			; There are two teams windows, the main win and the meeting win, break the loop so that the mute commmand doesnt get sent twice
			break
		}
	}
}
return

Search highlighted text

This script enables searching any text you have highlighted, in any program. This will open your default browser and perform a search using Google. You can modify it to use your preferred search engine.

;--------------------------------------------------------------------
; Google the highlighted selection
;--------------------------------------------------------------------

^+q::
{
	Send, ^c
	Sleep 50
	Run, https://www.google.com/search?q=%clipboard%
	return
}
return

Run / Activate / Minimize Window

I use the LaunchShowOrHide() helper function mainly for launching and showing Spotify, as shown below. You can use this helper function for various programs.

;--------------------------------------------------------------------
; Run / Activate / Minimize Window
;--------------------------------------------------------------------

LaunchShowOrHide(WindowName, RunName)
{
	if WinActive("ahk_class " . WindowName)
	{
		WinMinimize
	}
	else if WinExist("ahk_class " . WindowName)
	{
		WinActivate
	}
	else
	{
		Run %RunName%
	}
	return
}

;--------------------------------------------------------------------
; Application Hotkeys
;--------------------------------------------------------------------

^+LWin:: LaunchShowOrHide("SpotifyMainWindow", "C:\Users\YourUsernameHere\AppData\Roaming\Spotify\Spotify.exe")

Is there an AutoHotkey script you use frequently? Share in the comments!

#productivity#technology
An unhandled error has occurred. Reload 🗙