Mapping keyboard shortcuts to mouse clicks

Like most in the software development world, I'm allergic to the mouse, but there are times when no keyboard shortcut is available. I'm willing to jump through a lot of hoops for my keyboard shortcuts. As an example, I'll show you how I configured OSX so that I can see my prior locations in a browser back button with a keyboard shortcut (in this case, Ctrl+Cmd+Z). Chromium open issue 328709 requests this functionality.

First off, you need a tool which can move your mouse programmatically. There are a few tools which will do that, but the two main ones I encountered were xdotool for the ancient Unix/Linux X Window System and cliclick for OSX. After fiddling with xdotool, I settled on cliclick.

Using OSX's Automator, you can do a lot of cool things.1 I'm no expert with Automator and find it incredibly confusing, but I stumbled around into Utilities -> Run Shell Script and created a .workflow 'service' file saved at ~/Library/Services:

This will now show up at System Preferences->Keyboard->Shortcuts->Services, where I mapped it to Cmd+Ctrl+Z.

Whew. And yet that's not the end of the story, as cliclick did not do a right-click. So I installed BetterTouchTool (view homebrew cask, of course) and mapped the key which cliclick triggered to right-click. I hit Cmd+Ctrl+Z at least a few times per day (sometimes dozens) and it saves me about a couple seconds versus the alternative, so I feel it was worth the set up time.

  1. I foolishly experimented bash's bind for binding a key to cliclick, but of course that wasn't available outside of the terminal. I also played around with Karabiner (KeyMapRemap4MacBook)