Increase Keyboard Functionality with AutoHotkey

This tutorial is meant to be an introduction to AutoHotkey by exploring its most popular use: hotkeys.

Nowadays, one big selling point of keyboards is “on the fly macros” and reconfigurable keys. These allow you to save time on repetitive tasks and complicated shortcuts but can fall short on expectations by having limited functionality, besides the fact that they are usually quite expensive. This is where AutoHotkey comes in, allowing you to customize hotkeys, create macros and launch programs (and so much more) with any kind of keyboard.
AutoHotkey is a free and open-source custom scripting language for Windows, easy to learn, and utilizes almost no resources on a computer. It is also excellently documented, and with so much functionality that falls out of the scope of this tutorial, it’s not hard to get into the more advanced stuff.

Parts Required

Getting Started

By itself, AutoHotkey does nothing. After the installation, you need to create a script. To do this, right-click on an empty space in the folder where you want the script to be, select New then AutoHotkey Script and give it a name. To make sure everything is working properly, right-click the script and select Edit Script or Open with and select your text editor of choice (a list of editors with AutoHotkey syntax highlighting can be found here) and add the following code:

; Ctrl+j -> Trigger a windows notification
^j::TrayTip, Hey, You got this!, 60, 17

Save the script, then double-click it (or right-click and select Run Script). You should now have a new tray icon. Now press Ctrl and j on your keyboard and a notification will be triggered.
The TrayTip command is great to test new code and especially to debug more advanced functionalities.

Hotkeys

Now that everything is set up and working, let’s move on to basic usage. The syntax for a Hotkey is: Trigger::Target
The Trigger can be a single key or a combination of keys. The full list of key names and prefixes can be found here. When using modifiers (Ctrl, Shift, etc) it’s best to use their key prefix rather than the name. The basic modifier prefixes are exemplified in table 1, where you can see that the modifier position on the keyboard (left or right to the spacebar) is indicated by the “<” or “>” key, respectively. If the Target is a sequence of keys, use the Send command.

prefixdescription
<+Left Shift
>! Right Alt
>^Right Ctrl
<#Left Win
<^>!AltGr
Table 1

Examples (all lines that start with “;” are comments):

; Change the Pause key to the Media Play/Pause key
Pause::Media_Play_Pause

; Make the PrtSc key open the snipping tool (shortcut Win+Shift+s)
PrintScreen::Send, #+s

; Map LShift+Space to the Enter (Return) key 
; (this one is personal because I like to be able to hit Enter with my left hand)
; Here the Enter key is surrounded by {} because otherwise a hotstring is triggered
>^Space::Send, {Enter}

; Shut down the computer by pressing LWin+Esc
; Here the target is a large sequence of keys, so it's good practice to surround it 
; with curly braces
<#Esc::
{
	Send, #x          ; LWin+x
	Sleep, 400        ; Wait 400ms
	Send, {Up}
	Send, {Up}
	Send, {Right}
	Send, {Down}
	Send, {Down}
	Send, {Enter}
	return
}

As you can see in the above examples it can be a little tricky to make a hotkey do exactly what you want, so when in doubt head over to the documentation, and with some experimentation you’ll get there.

Shortcuts

Having a shortcut key assigned to an application icon is pretty standard, but what happens when you update the application? The shortcut key is gone and you have to assign it again. So let’s see how it’s done in AutoHotkey by using the Run command.

; Launch Sublime Text with AltGr+t
<^>!t::Run C:\Program Files\Sublime Text\sublime_text.exe

; Launch Notepadd++ with AltGr+n
; Note that here a directory is not given. In this situation AutoHotkey will search for an
; application with the given name and launch it if there is a match
<^>!n::Run notepad++.exe

; Open the Downloads folder with AltGr+d
<^>!d::Run C:\Users\USER\Downloads

Context-sensitive Hotkeys

If you want a hotkey to do something different based on the application being used, or to only work when a certain application is open, the context of the hotkey can be defined to best suit your needs. This can be done in a couple of different ways, the first is to check the currently focused application by name (a great tool to get information on an application is the Window Spy which can be opened by right-clicking the script logo on the taskbar, selecting Window Spy and hovering the mouse on the intended application).

; If the Spotify application is being used (has focus), map F10 to Volume Mute
#IfWinActive ahk_exe Spotify.exe
	F10::Send {Volume_Mute}
; This next line will turn off context sensibility, so that all other cases can be processed
#IfWinActive 

Another way is to search some text on the application title bar.

; Setting SetTitleMatchMode to 2 will make the search return true if the text is found anywhere
; in the title
SetTitleMatchMode, 2


; If the text "YouTube" is present in the window title, map F11 and F12 to control the volume
#IfWinActive YouTube
	F11::Send {Volume_Down}
	F12::Send {Volume_Up}
#IfWinActive

If the intended context is, for example, everything except text editors, you can define a group of applications like in the following example.

; Add Sublime Text to a group named text_editors
GroupAdd, text_editors, ahk_exe sublime_text.exe
; Add Notepad++ to the text_editors group
GroupAdd, text_editors, ahk_exe notepad++.exe

; If the active window is not in the text_editors group, map LCtrl+Space to the Enter (Return) key
#IfWinNotActive ahk_group text_editors
	<^Space::Send, {Enter}
#IfWinActive

If you want to get a better idea of a full AutoHotkey script you can check out mine on github.

Conclusion

Whether you want easier access to functionalities or automate a task, using AutoHotkey can greatly improve productivity as well as reduce fatigue from fiddling around with mouse clicks and remembering non-intuitive key combinations.

References

https://www.autohotkey.com/docs/AutoHotkey.htm

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock