DEV Community

Cover image for Embrace Keyboard Macros into your Dev Life with AutoHotKey - Part 1
Radu-Alexandru B
Radu-Alexandru B

Posted on • Edited on

Embrace Keyboard Macros into your Dev Life with AutoHotKey - Part 1

Cover image by Karol D, on Pexels.com

Contents:

What is AutoHotKey?

Without any blah-blah, AutoHotKey (AHK) is a scripting/interpreted language, that you can write in a script that will continuously run in the background when you open it (but don't worry, you have 100% control of this script). Every function that you'll write will have a set of instructions that will trigger on a keystroke or a certain combination of keystrokes that you press at a time.

Okey-dokey, so what?

This means you can create macros (with key shortcuts) that activate some certain actions, like:

  • select a word -> copy it -> open google on new chrome tab -> paste that word -> search it and show google results (all in less than 200milisecods)
  • open a new tab with your favorite website
  • open command prompt using CTRL+ALT+T shortcut
  • open command prompt with the path of the currently selected directory and run a command (like npm start or git status)
  • Adjust volume while holding a key+scrolling mouse wheel (the key could even be one of the side keys of your mouse, more on that later)
  • Empty recycle bin by pressing WIN+DEL (or any key combo you wish), anytime, anywhere
  • Put PC into sleep mode (if you don't have the FN-Sleep key)
  • Auto rewrite 'brb' to 'be right back'... or...
  • Autocomplete a for loop (or anything) syntax (any language, any text editor) just by writing forjs, forc, ifelsepy, or any hotstring you'd ever want to use
  • The sky is your limit, also the docs and the community are really great. There are high chances that any question you'd have, you'll find it answered.

Installation

Simply download and run the setup from AutoHotKey.com.

Keyboard keys and combinations that you can map/remap

You can remap any key/combination keys or map any combination of keys without a default function. Here are some of my key/keys combination that I recommend mapping:

Numpad

1) You can remap your Numpad keys (if you don't use them), or you can remap your Numpad keys while Numlock is off.

With Numlock off With Numlock on
NumpadIns Numpad0
NumpadEnd Numpad1
NumpadDown Numpad2
NumpadPgDn Numpad3
NumpadLeft Numpad4
NumpadClear Numpad5
NumpadRight Numpad6
NumpadHome Numpad7
NumpadUp Numpad8
NumpadPgUp Numpad9
NumpadDel NumpadDot

In total, we can have up to 22 keys to assign macros with our Numpad.

2) You can map (unused) keyboard combinations like:

  • RAlt+(anyKeyLetter) or RAlt+(number)
  • CTRL+Alt+(anyKey) or even CTRL+SHIFT+ALT+(anyKey)

So, we have lots of alternatives when choosing our future keys to have macros.

Mapping our first keys

Ok, enough talking - let's get into code and actually do something!

To start writing a script we just need to create a new file with a .ahk extension. The first things we need to have in mind are some key notations we will use:

Key notation in AHK Actual Key
^ Control
+ Shift
! Alt
# Windows Key

Now, let's make some of the macros I mentioned earlier:

  • Let's say, we want to map CTRL+ALT+T to open our command prompt, just like in Linux In our newly created myMacros.ahk file (that you can open with Notepad, Notepad++ or any text editor), we just need to write the following:
^!t::Run cmd
Enter fullscreen mode Exit fullscreen mode

That's all! Save it, and, if you already installed AutoHotKey, you can double-click this new file, and a small green H icon will pop up:

Alt Text

This means our script is now running in the background! And, if you press CTRL+ALT+T (^!t in AHK's notation), you should see the Command Prompt that opens. Nice! Let's do more. Reopen myMacros.ahk, and let's add some more macros.

  • Let's say, any time you press CTRL+ALT++SHIFT+T, we open our PowerShell (instead of our command prompt)... But also, let's make it that every time we open our PowerShell, we want it opened to C:\Users\MyUserName:
; Also, this is a comment in AHK, it starts with ; and can be added anywhere

; use ctrl+alt+t to open Cmd Prompt in C:\Users\Radu location
^!t::Run cmd, C:\Users\Radu

; use ctrl+shift+alt+t to open PowerShell
^+!t:: Run powershell, C:\Users\Radu 
Enter fullscreen mode Exit fullscreen mode

Perfect, save it and click on the Reload this script option (that we saw earlier) from the taskbar.

  • Let's say that we want to open Google.com or Dev.to every time we press CTRL+ALT+1 or CTRL+ALT+2. Just add to our existing code, these lines, then reload:
^!1::Run "https://www.google.com" ; use ctrl+alt+1
^!2::Run "https://dev.to"         ; use ctrl+alt+2
Enter fullscreen mode Exit fullscreen mode
  • Let's say that we want to adjust our Main Volume every time we are holding ALT while scrolling with our mouse wheel. Add to our existing code, these lines, then reload:
Alt & WheelUp::Volume_Up          ; use Alt+MouseWheel to adjust volume
Alt & WheelDown::Volume_Down
Enter fullscreen mode Exit fullscreen mode

Pretty good stuff so far, I'd say... But, we can do even more.

Alt Text

  • Let's say that.. every time we select any text, we want to search for it on Google immediately by pressing CTRL+ALT+C... Well, now we are going to write an entire function, within curly brackets, that ends with Return. Let's add these lines to our myMacros.ahk, save and reload:
; use ctrl+alt+c to search on Google the currently selected text
^!c::
{
  Send, ^c
  Sleep 50
  Run, https://www.google.com/search?q=%clipboard%
  Return
}
Enter fullscreen mode Exit fullscreen mode

Awesome!

  • Let's empty recycle bin by pressing WIN+DEL:
#Del::FileRecycleEmpty ; use win+del
Enter fullscreen mode Exit fullscreen mode
  • Let's put our PC in Sleep mode by using RightALT+s. Note that we can also be specific about our choice of keys by saying the exact name of the key (RAlt) in combination (&) with s:
RAlt & s:: DllCall("PowrProf\SetSuspendState", "int", 0, "int", 0, "int", 0)
Enter fullscreen mode Exit fullscreen mode
  • Finally, let's open the command prompt in our currently selected folder. To do that, we know that by pressing Alt+d when having a folder opened will focus on folder's path. So our steps sound like this: Focus on folder's path, copy it to clipboard, run cmd within that path. So, this is the function we are looking for:
^!u::     ; Use ctrl+alt+u to open cmd in current selected folder
{
  Send, !d
  Send, ^c
  Sleep 50
  Run cmd, %clipboard%
  Return
}
Enter fullscreen mode Exit fullscreen mode

Now, open a folder, press CTRL+ALT+U, and see what happens. It's a really handy little shortcut, eh?

We can even make it more powerful by saying to write a specific command for us, like git status, and also execute it:

^!i::     ; Use ctrl+alt+i to open cmd in current selected folder
{
  Send, !d
  Send, ^c
  Sleep 50
  Run cmd, %clipboard%
  Sleep 100
  Send, git status
  Sleep 100
  Send, {Enter}
  Return
}
Enter fullscreen mode Exit fullscreen mode

Okay now, we are approaching the end of this post. Bellow, you'll find our full code from myMacros.ahk with all the things we accomplished.

And that's it... for now! In the next part of this post, we will use AutoHotKey to generate some autocompletion on each keyword (Hot String) that we'll write. We will also look and some macros that we can use with a mouse that has up to 9 buttons (the ones with 3 side buttons).

However, until then... There are a lot (I mean, a lot) of other articles on AutoHotKey with the best macros ever imagined. AutoHotKey is a scripting language, so it has variables, for loops, conditionals, booleans, and even more... So it's a lot to take in.

I'll leave here some more interesting articles on this topic:

Full code from this post

; List of keyboard buttons
; ^ = Control
; + = Shift
; ! = Alt
; # = Win (Windows key)

^!t::Run cmd, C:\Users\Radu                    ; use ctrl+alt+t to open Cmd Prompt in C:\Users\Radu location
^+!t:: Run powershell, C:\Users\Radu           ; use ctrl+shift+alt+t to open PowerShell

; use win+del to empty recycle bin
#Del::FileRecycleEmpty

; use RightAlt+s to put PC on sleep mode
RAlt & s:: DllCall("PowrProf\SetSuspendState", "int", 0, "int", 0, "int", 0)

^!1::Run "https://www.google.com"              ; use ctrl+alt+1
^!2::Run "https://dev.to"                      ; use ctrl+alt+2
^!3::Run notepad.exe, WinActivate notepad.exe  ; Open and focus on notepad.exe

Alt & WheelUp::Volume_Up                       ; use Alt+MouseWheel to adjust volume
Alt & WheelDown::Volume_Down

^!c::     ; use ctrl+alt+c to search on Google the currently selected text
{
  Send, ^c
  Sleep 50
  Run, https://www.google.com/search?q=%clipboard%
  Return
}

^!u::     ; Use ctrl+alt+u to open cmd in currently selected folder
{
  Send, !d
  Send, ^c
  Sleep 50
  Run cmd, %clipboard%
  Return
}

^!i::     ; Use ctrl+alt+i to open cmd in currently selected folder
{
  Send, !d
  Send, ^c
  Sleep 50
  Run cmd, %clipboard%
  Sleep 100
  Send, git status
  Sleep 100
  Send, {Enter}
  Return
}
Enter fullscreen mode Exit fullscreen mode

Oh, almost forgot: I mentioned that you can use single key macros using the Numpad (with or without NumLock switched on). For all the macros mentioned earlier, just replace the key combo with a Numpad key, like so:

NumpadIns::Run cmd, C:\Users\Radu                    ; use Numpad 0 (NumLock off) to open Cmd Prompt

NumpadDel::     ; use Numpad dot (NumLock off) to search on Google the currently selected text
{
  Send, ^c
  Sleep 50
  Run, https://www.google.com/search?q=%clipboard%
  Return
}

; etc
Enter fullscreen mode Exit fullscreen mode

Okaay. Twas a long post. Have a nice day. Bye.
R.B.

Top comments (3)

Collapse
 
ruind124 profile image
ruind124

I don't remember what made me choose research paper help as a major. The devil made me do it; nothing else could. You may think that I probably love this discipline and do well in it, but you're wrong. I like discovering new data and so on, but not so much have devoted all my life to it. That's why I juggle studies with a part-time job and searching for something I would like more.

Collapse
 
zechariah66 profile image
Zechariah Conn

I've been using it for a few months now, and I'm very happy with it. It's easy to setup, and has all the basic features of a keyboard macro. I can easily set up macros to do many different things, including run external programs (like VS), open files or URLs and even perform simple text substitutions. It is suggested to be a useful masterbundles.com/templates/presen... source for premium templates. It's great for those who want to take control of their computer without having to learn programming language commands or memorizing large amounts of code.

Collapse
 
luisharp22 profile image
Info Comment hidden by post author - thread only accessible via permalink
luisharp22

It is only us that you can trust to offer you professional management assignment help throughout the entire duration of your career. The team of writers that we have is made up of highly proficient people with vast experience in handling various kinds of management assignments. In fact, there is no management paper that can be too complicated for us to handle. Our writers on essaywritinghelp.pro/write-my-paper/ can always offer you help with management papers for essays, dissertations, reports, term papers and coursework.

Some comments have been hidden by the post's author - find out more