Friday, 25 October 2013

Use all the keys on your keyboard - AHK

How often do you actually use the CAPSLOCK key? What about the "other" Alt and Control keys on the right-hand side of your keyboard? Or the AppsKey? Or the the much-hated "Windows" key? What about the left-right-up-down functions of your number pad when numlock is turned off?

Imagine if they could all easily be modified to do something useful.

Enter AutoHotkey (AHK), a free and very simple scripting language, which is designed to help you use your keyboard more efficiently. It can do anything from a simple remapping (make the 'a' key send a 'b' command, the 'b' key send a 'c' command, and annoy your friends and enemies), to creating full-blown GUI Applications.

Today, I'll be describing how you can use AHK for some simple window manipulation. Overusing a mouse or a touchpad leads to RSI (Repetitive Stress Injury), and also each time your fingers leave the keyboard you're wasting valuable time. Time you could be using in order to stop wasting time...

I'm assuming that you already know about the Windows shortcuts such as:
Alt + Tab switches between open applications
Win + Tab switches between open applications with a wheel-like graphic
Alt + F4 closes active window
Win + d shows the desktop

AutoHotkey is fairly straightforward to install. Head to the home page, press the download button, and run the .exe file. Once you've installed it, you should be able to right click on your desktop, select "New" and choose AutoHotkey Script from the menu. Name the file something like "hot keys", right click on it, and press "edit" to open it in notepad (Or press edit in Notepad++ if you have that installed).

So, let's start out by making use of one of the easiest to reach keys on a standard keyboard: The CapsLock key. We're not going to rob it of its primary function, so if you enjoy SHOUTING AT PEOPLE on forums, or you're a post-modernist writer who uses lots of block capitals to 'express yourself', fear not.

I often switch through a bunch of open applications, closing ones I don't need anymore. Normally this requires a bunch of Alt + Tabs interspersed by Alt + F4s, which is awkward as your middle finger keeps switching between the distant Tab and F4 keys, and your cramped thumb starts complaining that this wasn't what it went through all that evolution for.

Adding the lines below to your newly created AHK script will save you time and pain.

CapsLock & r::AltTab
CapsLock & e::ShiftAltTab
CapsLock & Space::WinClose,A

This means that holding down the CapsLock key and pressing 'r' will switch between your open applications. CapsLock and 'e' will do the same, but in reverse order. And most useful of all, pressing CapsLock and Space will close the active window. Now you never have to reach for that F4 key again. Just make sure that you release and repress the CapsLock key in between switching between applications and pressing the space bar, otherwise you'll end up quitting the Alt-Tab menu itself, and you won't be able to use Alt-Tab or Capslock + 'r' anymore (if you do do this by accident, the easiest way to restart the functionality is by logging out of windows and then back in again).

That was fun. But let's get a bit more inventive, and add some basic arithmetic. Windows 7 does a pretty neat job of snapping an application to exactly half the screen when you drag a window the the left or right, but it's a bit restrictive and it requires using the mouse, which we're trying to avoid. The few lines below will do a bit of arithmetic based on the size of your screen and snap the active window to the right or the left when you press Capslock and 'f' (right) or Capslock and 'd' (left). Note the "-40", which is to account for the 40 pixels that the taskbar needs. This works on a 15.6" laptop monitor with a standard sized Windows 7 taskbar. If you have your taskbar set to use small icons, or have a different sized monitor you'll need to play around with this number a bit to get it right. If your taskbar is set to auto-hide, you can remove it completely.

CapsLock & f::
{
WinRestore,A
WinMove,A,,A_ScreenWidth/2,0,A_ScreenWidth/2,A_ScreenHeight-40
return
}

CapsLock & d::
{
WinRestore,A
WinMove,A,,0,0,A_ScreenWidth/2,A_ScreenHeight-40
return
}

Now let's take that idea a bit further: the following lines will let you use CapsLock + 1, CapsLock + 2, CapsLock + 3, and CapsLock + 4 to make the active window take up exactly one quarter of the screen.

CapsLock & 1::
{
WinRestore,A
WinMove,A,,0,0,A_ScreenWidth/2,A_ScreenHeight/2
return
}
CapsLock & 2::
{
WinRestore,A
WinMove,A,,A_ScreenWidth/2,0,A_ScreenWidth/2,A_ScreenHeight/2
return
}
CapsLock & 3::
{
WinRestore,A
WinMove,A,,0,A_ScreenHeight/2,A_ScreenWidth/2,A_ScreenHeight/2-29
return
}
CapsLock & 4::
{
WinRestore,A
WinMove,A,,A_ScreenWidth/2,A_ScreenHeight/2,A_ScreenWidth/2,A_ScreenHeight/2-29
return
}

Now you can use use GMail, Facebook and VLC Media player, while doing some work in Microsoft Word. Isn't that productivity. And better still, try this. Put VLC in full-screen mode (open a movie and press F11), then press CapsLock + '2'. VLC will snap to the top right corner of screen, but remain in full-screen mode, so you won't have to sacrifice screen space to menu bars and borders. If you don't know what VLC is, it's the best media player out there, and it doesn't constantly complain about not being able to play your music and video files because they're in the wrong format: it plays anything*.



I can watch Top Gear and write at the same time...

Let's just add two more lines to our script:

CapsLock & m::WinMinimize,A
CapsLock & g::WinMaximize,A

So now we can maximize and minimize windows without the mouse as well. Just press CapsLock + 'm' to minimize the active window, and Capslock + 'g' to make the current window full size. And that's the end of window manipulation for now, but the possibilities of AHK are endless. My current main AHK script is over a thousand lines long. Check back soon for some hints on how to make typing more efficient, also using AHK.

If installing AHK is too much mission, here is a link to an .exe containing all the shortcuts mentioned in this post, which should run on any Windows PC without any installation. If you do get around to installing AHK, here is the script in an .ahk file, so you can download it and edit it to suit your needs.



* Well, maybe not everything. But why would we want to get technical in a blog with this one's title?

No comments:

Post a Comment

About Me

My photo

I'm far away from home in this country called "Europe". I'm studying towards a Master's in Computational Linguistics (I think - this might help: https://xkcd.com/114/). I write about web applications and Python and other things that you may find interesting (considering you got this far).