HomeMathComputingArtsWordsLiteratureMusictwitter facebook webfeed

Windows Programing: AutoHotkey Example Scripts

Advertise Here For Profit

Xah Lee, 2009-08-25, …, 2011-04-23

This page is a collection of useful examples of AutoHotkey (ahk). If you are not familiar with ahk, see: AutoHotkey Basics.

Switching Apps

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

; single key to activate Flip 3D for switching apps. 
$F1::Send ^#{Tab} ; Ctrl+Win+Tab
; F2 for previous window/app
$F2::Send !{Tab}

Disable Caps Lock, Num Lock, ScrLk

The Num Lock and ScrLk keys are almost never used. The Caps Lock is also a problem for many people because it is easy to hit by accident. You can disable them.

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

; disable them
$NumLock::Return
$ScrollLock::Return
$CapsLock::Return

See also: How to Swap Caps Lock, Alt, Control Keys on Windows.

Disable the Windows Logo Key

You can disable the Window key.

;; disable Win key behavior of popping up the Start Menu
;; but Win+‹key› should still work
~LWin Up::Return
~RWin Up::Return
;; disable some Win+‹key› combination
#1::Return ; disable Win+1
#r::Return ; disable Win+r
#e::Return ; disable Win+e
;; disable the Win key completely
Lwin::return ; disable left Win
Rwin::return ; disable right Win

However, the 【Win+l】 (lock computer) cannot be disabled. (possibly a few others)

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.

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+NumpadAd increase sound volume
$#NumpadAdd::Send {Volume_Up 5} ; increase volume
$#NumpadSub::Send {Volume_Down 5} ; decrease sound level

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:

#NoTrayIcon
Send {Volume_Up 5}

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

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

; F1 to activate volume change control on the Taskbar notification area
; then, use mouse scroll wheel to change volume
$F1::Send #b{Left}{Left}{Enter}

Closing Tabs 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

Closing The 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 closing the current visible box. You can define a easy key, so that it'll close the current browser tab, or window, or application. Here's a example:

;;; 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”. 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.

Launching or Switching To Browser

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

; F7 to launch or switch to Firefox

$F7::
IfWinExist ahk_class MozillaUIWindowClass
{
  WinActivate
}
Else
{
  Run "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
  WinWait ahk_class MozillaUIWindowClass
  WinActivate
}
Return

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

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.

More Examples

Here's my hotkey setup main file. xah_autohotkey.ahk.

blog comments powered by Disqus