Useful AutoHotkey Scripts

By Xah Lee. Date: . Last updated: .

This page is a collection of useful AutoHotkey scripts.

[see AutoHotkey Tutorial]

Swap Modifier Keys

;; swap Alt and Ctrl
;; this doesn't work perfectly, when you need to press Shift or others with Alt or Ctrl

[see AutoHotkey Key Syntax]

; make the right Win key behave as AppsKey

Launch or Switch to Browser

Suppose you want a hotkey that launches a browser, but if it is already running, just switch to it.

; launch or switch to Firefox
; version 2021-02-21
IfWinExist ahk_class MozillaWindowClass
 WinActivateBottom, ahk_class MozillaWindowClass
 Run "C:\Program Files\Mozilla Firefox\firefox.exe"


Alternatively, pin the apps you want to the taskbar, then, ❖ Window+1 will launch or switch the left most one. ❖ Window+2 for the second one, etc. Then, you can set up a single key to press the keychords.

; make F1 send Win+1
$F1::Send #1

; make F2 send Win+2
$F2::Send #2

Launch or Switch to Emacs

; launchOrSwitchEmacs
; version 2021-02-21
IfWinExist ahk_class Emacs
  WinActivate, ahk_class Emacs
  Run "%HOMEPATH%\Downloads\emacs-27.1-x86_64\bin\runemacs"


How to find window name or app name?

To find out what is the window's ahk_class , right click on the AHK icon and chose Window Spy , then click on a window you want.

win 10 AutoHotkey window spy 2021-02-14 QftPt
AutoHotkey Window Spy 2021-02-14
win 10 AutoHotkey window spy 2021-02-14
AutoHotkey Window Spy window

Switch Apps

It is useful to have a single button to switch to last window.

; set Left Windows Key for switching to previous window/app
$LWin::Send !{Tab}

Single Key for Cut, Copy, Paste

$F2::Send ^x ; cut
$F3::Send ^c ; copy
$F4::Send ^v ; paste

Empty Trash (Recycle Bin), Open Trash

$#Del::FileRecycleEmpty ; Win+Del to empty trash (recycle bin)

open recycle bin:

$F5::RUN ::{645ff040-5081-101b-9f08-00aa002f954e} ; open trash (recycle bin)

Changing Sound Level

Many keyboards have special buttons to control sound level, but they are a pain to use. Because, the button change volume by a tiny amount. You have to press it 5 or 10 times. If you hold it, for a second nothing happens, then all of a sudden it became too loud.

The following script changes volume by 10%, using Ctrl and the + and - on the numberpad.

;; Win+NumpadSub decrease sound volume, Win+NumpadAdd increase sound volume
$#NumpadAdd::Send {Volume_Up 5}
$#NumpadSub::Send {Volume_Down 5}

Change Sound Level with Multimedia Key

If you have a keyboard with multimedia keys such as Microsoft Ergonomic Keyboard, and want increase sound volume by one of its special button, here's what you can do.

Create a file of the following content and save it as increase_sound.ahk:

Send {Volume_Up 5}

Then, use IntelliType to assign the keyboard's special button to launch the script.

Close Tab in Browser

; Pause/Break key closes browser tab
Pause::Send ^w ; close browser tab

; make F11 and F12 do switch tabs in browsers
$F11::Send ^{PgUp}
$F12::Send ^{PgDn}

Close Current Window or Tab

Windows has many different keyboard shortcuts to close the window. For example, Alt+F4 closes the application, Ctrl+F4 or Ctrl+w closes current tab or window in browsers, Escape usually closes dialog boxes. (See: Windows Keyboard Shortcuts) All these can be thought of close the current visible box. You can define a easy key, so that it'll close the current browser tab, or window, or application.

;;; make the Pause/Break key to close window or tab.
; which key to send depends on the application
IfWinActive ahk_class ATH_Note
{ ; ATH_Note is Windows Mail
; Ctrl+F4
  Send !{F4}
IfWinActive ahk_class Notepad
{ ; Alt+F4
  Send !{F4}
Else IfWinActive ahk_class Outlook Express Browser Class
{ WinMinimize, A
Else IfWinActive ahk_class IrfanView
{ Send {Esc}
Else ; IE, Firefox, Google Chrome, Opera
{  Send ^w

The code checks what application is the current window, by the IfWinActive lines. When you want a hotkey to check what is the current window or if it exists, you can use “IfWinActive” or “IfWinExist”.

Toggle Window Size

AutoHotkey: Toggle Maximize Window

Hide/Minimize Window

; make Insert key to hide (minimize) current window
$Insert::WinMinimize, A

Note: in Microsoft Windows 7, you can maximize by ❖ Window+ and restore by ❖ Window+. [see Windows Keyboard Shortcuts]

Temporarily Disable AutoHotkey

You have many personal hotkeys. In some situations, you want these hotkeys to be off temporarily. You can create a hotkeyX, so that it'll disable all your hotkeys, and press hotkeyX again to turn all your hotkeys back on. Here's a example:

; make the scroll lock key (ScrLk) toggle all hotkeys.

When hotkeys are suspended, the AutoHotkey icon in your system notification area changes to S.

Swap Middle/Right Mouse Buttons

; swap middle/right buttons

[see Why Swap Mouse Right Button and Middle Button]

If you have a question, put $5 at patreon and message me.

AutoHotkey Tutorial

AutoHotkey logo nz9fX