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
Ctrl::Alt
Alt::Ctrl

[see AutoHotkey Key Syntax]

RWin::AppsKey ; make the right Win key behave as AppsKey

Launch Browser, Switch to Browser

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

; F7 to launch or switch to Firefox
$F7::
IfWinExist Mozilla Firefox
{
    WinActivateBottom, Mozilla Firefox
}
Else
{
  Run "C:\Users\Public\Desktop\Mozilla Firefox.lnk"
}
Return

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.

AutoHotkey Window Spy
AutoHotkey's “Window Spy” window.

for Mac or Linux, see: How to Set Key to Switch to Browser

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}

Alt+Tab】 is no good because it strain your hands, and you have to take a second to chose.

Single Key for Cut, Copy, Paste

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

Disable Caps Lock, Num Lock, ScrLk

; set the default state of the lock keys
SetCapsLockState, off
SetNumLockState, on
SetScrollLockState, off

; disable them
$NumLock::Return
$ScrollLock::Return
$CapsLock::Return
; make Caps Lock key do Control
CapsLock::Ctrl

[see AutoHotkey Key Syntax]

; make the capslock key behave as menu/AppsKey
CapsLock::AppsKey

The ▤ Menu key is actually very useful. See: Microsoft Keyboard Menu Key.

Disable Windows Logo Key

Disable Win Key Start Menu Behavior

Disable ❖ Window key, but don't disable 【❖ Window+‹key›】 combination.

;; disable Win key behavior of popping up the Start Menu, but don't disable Win+‹key› combination
~LWin Up::Return
~RWin Up::Return

Disable Win+key Combination

;; disable some Win+‹key› combination
#1::Return ; disable Win+1
#r::Return ; disable Win+r
#e::Return ; disable Win+e

Note: the 【❖ Window+l】 (lock computer) cannot be disabled.

[see Windows Logo Key Keyboard Shortcuts]

Disable Win Key Completely

;; disable the Win key completely
Lwin::return ; disable left Win
Rwin::return ; disable right Win

Disable F1 Help

#F1::Return ; disable Windows Help

Instead of disable keys, it's better to set them to do something useful, such as switching app/window/tab, or copy cut 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} ; increase sound level
$#NumpadSub::Send {Volume_Down 5} ; decrease sound level

Change Sound Level with Multimedia Key

If you have a multimedia keyboard with special 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:

#NoTrayIcon
Send {Volume_Up 5} ; increase sound level

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 to switch tabs in Firefox
$F11::Send ^{PgUp} ; prev tab
$F12::Send ^{PgDn} ; next tab

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: Microsoft 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
Pause::
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
 }
Return

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

[see AutoHotkey: Script to 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 Logo Key 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.
$ScrollLock::Suspend

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

Swap Middle/Right Mouse Buttons

; swap middle/right buttons
$RButton::MButton
$MButton::RButton

[see Why Swap Mouse Middle-Click and Right-Click?]

AutoHotkey

  1. AutoHotkey Tutorial
  2. AutoHotkey Key Syntax
  3. Useful AutoHotkey Scripts
  4. Swap Caps Lock, Alt, Control Keys
  5. Toggle Maximize Window

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

Windows Keyboard How-To

  1. Windows Keyboard Software Guide
  2. AutoHotkey Tutorial
  3. AutoHotkey Syntax
  4. AutoHotkey Scripts
  5. Swap CapsLock, Alt, Control
  6. Disable CapsLock, Win Key
  7. Win Key Shortcuts
  8. Key Shortcuts
  9. Console Shortcuts
  10. Swap Mouse Buttons