DEV Community

loading...
Cover image for Embrace Mouse Macros into your Dev Life to increase your Productivity with AHK - Part 2

Embrace Mouse Macros into your Dev Life to increase your Productivity with AHK - Part 2

radualexandrub profile image Radu-Alexandru B Updated on ・7 min read

Cover Photo by Davide Boscolo on Unsplash

Contents

Short Intro

As we saw in our previous part of this post, we can achieve quite a few things with AutoHotKey keyboard macros. In this part, we will focus more on using combinations of a 3-sided-button mouse. If you don't have such a mouse, well, don't worry, you can assign the following macros to any other keyboard keys you want (as discussed earlier), however, the prices of these type of mice are really cheap (starting from $10 for any "gaming" one, yet reliable). We'll later see some use-case scenarios for why such a mouse could be a real value for your general increase in productivity.

Mouse Macros we will create

Hoping that you skipped the long "short" intro, let's look into the mouse macros we'll create today (macros that don't require any interaction with a keyboard):

  • Adjust the main volume by holding a side key while scrolling
  • Play/Pause our current song with a side key (Youtube, Spotify, Apple Music, Mixcloud, anything.)
  • Screenshot an area with the help of LightShot
  • Copy any text/image and Paste it from clipboard
  • Scroll back and forth through (Browser/Text Editor) tabs by holding a side key while scrolling
  • Close a (Browser) tab
  • Reopen a closed Browser tab (that was closed accidentally or that we actually needed)
  • Search a selected text on Google (we will the same method as we saw in the previous post)

Mouse key we can map

Every mouse with 3 side buttons will have the default functions of Browser Go Back one page, Browser Go Forward, and a Control key. With AutoHotKey we can remap those Browser buttons. The Control button (set by default) is actually useful for selecting multiple files, zoom in/out, or select/edit multiple lines of code simultaneously in Sublime Text (In VSCode we would use Alt+Click).

Mouse with 3 side buttons

The AHK notations for all of our mouse buttons are the following:

Key notation in AHK Actual Mouse Key
LButton RButton MButton Left, Right, Middle buttons
WheelDown WheelUp Our scroll wheel, obviously
XButton1 4th mouse button, typically performs the Browser_Back function
XButton2 5th mouse button, typically performs the Browser_Forward function
WheelLeft WheelRight Not real buttons, but they can be used as hotkeys to scroll to left or right

Setting up a convention

Is totally up to you to choose the key combinations of the previously mentioned buttons. However, I've created a small convention that I'll use in this post:

Mouse macros convention

Mapping our mouse

Firstly, we need again to keep in mind the key notations in AHK for Control, Shift, Windows, and Alt:

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

And we'll also need to create a new .ahk file named mouseMacros.ahk (or we could just use the myMacros.ahk from the previous post and add the new macros there).

Finally, let's map our mouse. We'll start with our "5th mouse button" (XButton1) used for our Media macros.

  • To play/pause our currently playing media (song), we'll just write:
XButton1::Media_Play_Pause
Enter fullscreen mode Exit fullscreen mode
  • We will also write the following lines to adjust our main volume:
XButton1 & WheelUp::Volume_Up
XButton1 & WheelDown::Volume_Down
Enter fullscreen mode Exit fullscreen mode

Pretty straightforward I'd say. Let's save our file, and just double click it to open with AutoHotKey. The green 'H' icon will pop up letting us know that the script is running in the background:

Alt Text

Now, we can start playing our favorite songs/playlists and hold XButton1 while scrolling to see our volume changing.

Cool, let's add the next macros:

  • Presuming that we installed LightShot, a very handy screenshot tool for Windows, we'll use the Middle mouse button to take a screenshot. And, let's also write the Copy (with left-click) and Paste (right-click) functions, while holding XButton1:
XButton1 & MButton::PrintScreen         ; Take screenshot
XButton1 & LButton::Send, ^c            ; Copy
XButton1 & RButton::^v                  ; Paste
Enter fullscreen mode Exit fullscreen mode

Save this file, and click on "Reload This Script" option from the AHK icon.

Great! Next time when we are helping or asking somebody for help by sending a screenshot, we'll no longer take a picture with our phone, we'll just send a properly cropped screenshot instead, only by using our mouse!

Screenshot Copy Paste just by using Mouse

Let's now add macros to the XButton2:

  • By default, if we press CTRL+TAB (^Tab) or CTRL+SHIFT+TAB (^+Tab) in our Browser (eg. Chrome), we can go forward/backward one tab at a time. Let's add the following lines in order to go through tabs by scrolling with our mouse wheel (while holding XButton2):
XButton2 & WheelUp::^+Tab               ; Scroll through Browser tabs
XButton2 & WheelDown::^Tab
Enter fullscreen mode Exit fullscreen mode
  • We also know we can open Open a new tab (CTRL+T or ^t), close the current tab (CTRL+W or ^w), and reopen a closed tab (CTRL+SHIFT+T or ^+t). We will map these keys into our mouse like this:
XButton2::^t                            ; New tab
XButton2 & RButton::^w                  ; Close current tab
XButton2 & MButton::^+t                 ; Reopen last tab
Enter fullscreen mode Exit fullscreen mode

And, we will also use our useful macro to search on Google the currently selected text by pressing XButton2 and Left Click:

XButton2 & LButton::                    ; Search currently selected text on Google
{
  Send, ^c
  Sleep 50
  Run, https://www.google.com/search?q=%clipboard%
  Return
}
Enter fullscreen mode Exit fullscreen mode

Perfect. Let's save and reload the script... and also take it to a test drive!

Google Errors just by pressing a button

Et voilà, we've successfully mapped our mouse.

Productive Scenarios

Okay, long talk ahead. After all those mappings, you may ask: "How the hell will I remember all these key combinations? My mouse/keyboard feels now like a puzzle that can do more harm than good with a wrong key press"... and yes, I would understand that. But, once you really start using these macros (one at a time), you will start getting comfortable. In a short period of time (maybe days or weeks), those macros will become second nature to you.

Let's assume the following scenario: You are running a newly written code, but you encounter an error. Let's assume that your usual workflow is to search on Google for that error, open a few tabs, close other tabs, copy and paste some lines of code, and repeat until you solve that one error. Now, we all know that we usually encounter dozens of errors, therefore our majority of time is all about searching for fixes.

What if each time we encounter an error, we would execute the whole workflow just by some clicks with our mouse? while we are concentrating more on the solution?

Let's see how our earlier mapped macros could help us in this scenario:

  • First, we run our code, error pops up: We just select the whole error text with our mouse, click both sideClick+leftClick and google solutions opens
  • Now we are going to open a bunch of new tabs (using our middleClick) with some solutions (StackOverflow, GitHub, Dev, etc)
  • Now we are scrolling through tabs (sideClick+mouseWheel) and fast reading solutions, some of them may contain code that we might want to copy and paste (sideClick+left/rightClick) into our project
  • We are closing some of the irrelevant tabs with sideClick+leftClick
  • We might realize that we actually needed a previously closen tab: sideClick+middleClick
  • We might encounter a video tutorial on the way: sideClick (to pause the currently playing song), open tutorial, adjust volume (sideClick+mouseWheel)
  • Repeat (without even touching the keyboard)

Bonus Macro for Front-end Devs

Okay, here's one more useful macro for the front-end devs out there: We can copy to clipboard the HEX color of the current pixel under our cursor using CTRL+Win+LeftClick (or any other keys you want):

^#LButton::
{
  MouseGetPos, MouseX, MouseY
  PixelGetColor, color, %MouseX%, %MouseY%, RGB
  StringLower, color, color
  clipboard := SubStr(color, 3)
  Return
}
Enter fullscreen mode Exit fullscreen mode

And here's a showcase:

Copy Paste color under cursor

Full code from this post

XButton1::Media_Play_Pause
XButton1 & WheelUp::Volume_Up
XButton1 & WheelDown::Volume_Down
XButton1 & MButton::PrintScreen         ; Take screenshot
XButton1 & LButton::Send, ^c            ; Copy
XButton1 & RButton::^v                  ; Paste

XButton2 & WheelUp::^+Tab               ; Scroll through Browser tabs
XButton2 & WheelDown::^Tab
XButton2::^t                            ; New tab
XButton2 & RButton::^w                  ; Close current tab
XButton2 & MButton::^+t                 ; Reopen last tab
XButton2 & LButton::                    ; Search currently selected text on Google
{
  Send, ^c
  Sleep 50
  Run, https://www.google.com/search?q=%clipboard%
  Return
}

; Copy to clipboard the HEX color of the pixel under your cursor using CTRL+Win+LeftClick
^#LButton::
{
  MouseGetPos, MouseX, MouseY
  PixelGetColor, color, %MouseX%, %MouseY%, RGB
  StringLower, color, color
  clipboard := SubStr(color, 3)
  Return
}

Enter fullscreen mode Exit fullscreen mode

Okay done, that was it! In the next part, we'll finally cover AutoHotKey's HotStrings, hopefully as short as possible.

Keep improving yourself,
R.B.

Discussion (0)

Forem Open with the Forem app