MathCurvesSurfacesWallpaper GroupsGallerySoftwarePOV-Ray
ProgramingLinuxPerl PythonHTMLCSSJavaScriptPHPJavaEmacsUnicode ♥
Web Hosting by 1&1

Windows Programing: AutoHotkey Example Scripts

Xah Lee, , …,

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

Disable Keys

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

Disable Windows Logo Key

Disable Win Key Start Menu Behavior

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

However, the 【❖ Win+l】 (lock computer) cannot be disabled. (possibly a few others) See also: Windows Logo Key Shortcuts List.

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

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

Switching Apps

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

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

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"

You might need to add more {Left} in the above.

Closing 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

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

Swap Middle/Right Mouse Buttons

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

More Examples

Here's my hotkey setup files. https://github.com/xahlee/xah_autohotkey_scripts

blog comments powered by Disqus