Useful AutoHotkey Scripts

, , …,

This page is a collection of useful AutoHotkey scripts.

If you are not familiar with AHK, see: AutoHotkey Tutorial.

Swapping Modifier Keys

RWin::AppsKey ; make the right Win key behave as AppsKey
;; swap Alt and Ctrl
;; this doesn't work perfectly, when you need to press Shift or others with Alt or Ctrl

For more key syntax, see: AutoHotkey Key Syntax.

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
IfWinExist Mozilla Firefox
    WinActivateBottom, Mozilla Firefox
  Run "C:\Users\Public\Desktop\Mozilla Firefox.lnk"

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 for {Microsoft Windows, Mac, Linux}

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
; make Caps Lock key do Control

For more key syntax, see: AutoHotkey Key Syntax.

; make the capslock key behave as menu/AppsKey

The ▤ Menu key is actually very useful. See: What's the Use of the ▤ Menu/App Key?.

Disable Windows Logo Key

Disable Win Key Start Menu Behavior

Disable ❖ Win key, but don't disable 【❖ Win+‹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 【❖ Win+l】 (lock computer) cannot be disabled.

〔➤ Windows Logo Key Keyboard Shortcuts

Disable Win Key Completely

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

If you are using Microsoft Keyboards, you can just disable the ❖ Win key using the bundled IntelliType. (open it in the Keyboard control panel) But even with IntelliType, it still won't disable 【❖ Win+l】. See also: Microsoft IntelliType Hacks.

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:

Send {Volume_Up 5} ; increase sound level

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

Mouse Wheel for Sound Level

You can set mouse wheel to adjust volume, with a visible volume bar. In the following, press F1 starts the Volume Mixer, then you can use mouse scroll wheel to change with visual feedback.

; F1 to activate Sound Level control (Volume Mixer)
$F1::Run "C:\Windows\System32\SndVol.exe"

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, Esc 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
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

It would be great to have a key to maximize the current window to full screen size, and press again to restore to the original size. See: AutoHotkey: Script to Toggle Maximize Window.

You can also hide/minimize current window, like this:

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

Note: in Microsoft Windows 7, you can maximize by 【❖ Win+】 and restore by 【❖ Win+】. 〔➤ 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.

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

Swap Middle/Right Mouse Buttons

; swap middle/right buttons

〔➤ Why You Should Swap Mouse Middle-Click & Right-Click?

More Examples

Here's my hotkey setup files.

blog comments powered by Disqus