Embrace Mouse Macros into your Dev Life to increase your Productivity with AHK - Part 2
Tags: productivity, showdev, autohotkey, macros
Published: 2020-10-30 18:41:31

_<span>Cover Photo by <a href="https://unsplash.com/@impulse9696?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Davide Boscolo</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></span>_

## Contents
- [Short Intro](#ShortIntro)
- [Setting up Mouse Macros](#MouseMacros)
  - [What macros we will create?](#MouseMacros)
  - [Mouse buttons we can map](#MouseButtons)
  - [Setting up a convention](#Convention)
  - [Mapping our mouse](#MappingMouse)
  - [Productive Scenarios](#ProductiveScen)
- [Bonus Macro for Front-end Devs](#BonusMacro)
- [Full code from this post](#FullCode)

## <a name="ShortIntro">Short Intro</a>

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.

## <a name="MouseMacros">Mouse Macros we will create</a>

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***](https://app.prntscr.com/en/index.html)
- 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)


## <a name="MouseButtons">Mouse key we can map</a>

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***](https://www.sublimetext.com/) (In [***VSCode***](https://code.visualstudio.com/) we would use `Alt+Click`).

![Mouse with 3 side buttons](https://dev-to-uploads.s3.amazonaws.com/i/zc0t3zyz580kp7fvwol5.jpg)

The [AHK notations for all of our mouse buttons](https://www.autohotkey.com/docs/KeyList.htm#mouse) 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 |


## <a name="Convention">Setting up a convention</a>

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](https://dev-to-uploads.s3.amazonaws.com/i/khi392fi3gyy8or0tfoj.jpg)


## <a name="MappingMouse">Mapping our mouse</a>

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

- We will also write the following lines to adjust our main volume:

```
XButton1 & WheelUp::Volume_Up
XButton1 & WheelDown::Volume_Down
```

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](https://dev-to-uploads.s3.amazonaws.com/i/tnxsdvlu7ej6axn6s3w5.jpg)

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***](https://app.prntscr.com/en/index.html), 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
```

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](https://dev-to-uploads.s3.amazonaws.com/i/kb7m68r98t4yddqtg2u8.gif)

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

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

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
}
``` 

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

![Google Errors just by pressing a button](https://dev-to-uploads.s3.amazonaws.com/i/hrofkuazxy0q6ksixfb4.gif)

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

## <a name="ProductiveScen">Productive Scenarios</a>

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)


## <a name="BonusMacro">Bonus Macro for Front-end Devs</a>

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
}
```

And here's a showcase:

![Copy Paste color under cursor](https://dev-to-uploads.s3.amazonaws.com/i/b4iduhtg9d0910vvzovz.gif)

## <a name="FullCode">Full code from this post</a>

```
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
}

```

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.