Archive for the ‘hotkeys’ Category

Force-Quit AutoHotKey Script

Posted: June 30, 2011 by jellymann in computers, hotkeys, keyboard, windows

I noticed that on a Mac, you can force quit an application by pressing Command+Shift+Option+Escape. This is infinitely easier than opening the task manager and clicking to find the process.

Here is an AutoHotKey script I made that force-quits the active application using Control+Shift+Alt+Escape:

!^+Esc::
WinGet,win_n,ProcessName,A
WinGet,win_pid,PID,A
MsgBox, 4, Kill?, Kill %win_n% (%win_pid%)?
IfMsgBox, YES
    Run,taskkill /f /pid %win_pid%,,,
else
	return
return

Or if you prefer not to have a message box, and just plain kill the app:

!^+Esc::
WinGet,win_pid,PID,A
Run,taskkill /f /pid %win_pid%,,,
return

Easy Resize Script: Fix

Posted: February 4, 2011 by jellymann in computers, fixes, hotkeys, keyboard, mouse, windows

I forgot something on my last post. The Easy Window Resize script I showed you had the same labels as the original Easy Window Move, so it didn’t work unless you ran them on separate scripts. The simple fix is to rename all the labels to unique ones. Otherwise, you can just use the fixed one below. I replaced all “EWD” with “EWR” and changed all “LButton” to “MButton”.

#MButton::
CoordMode, Mouse ; Switch to screen/absolute coordinates.
MouseGetPos, EWR_MouseStartX, EWR_MouseStartY, EWR_MouseWin
WinGetPos, EWR_OriginalPosX, EWR_OriginalPosY,,, ahk_id %EWR_MouseWin%
WinGet, EWR_WinState, MinMax, ahk_id %EWR_MouseWin%
if EWR_WinState = 0 ; Only if the window isn't maximized
SetTimer, EWR_WatchMouse, 10 ; Track the mouse as the user drags it.
return

EWR_WatchMouse:
GetKeyState, EWR_MButtonState, MButton, P
if EWR_MButtonState = U ; Button has been released, so drag is complete.
{
SetTimer, EWR_WatchMouse, off
return
}
GetKeyState, EWR_EscapeState, Escape, P
if EWR_EscapeState = D ; Escape has been pressed, so drag is cancelled.
{
SetTimer, EWR_WatchMouse, off
WinMove, ahk_id %EWR_MouseWin%,, %EWR_OriginalPosX%, %EWR_OriginalPosY%
return
}
; Otherwise, reposition the window to match the change in mouse coordinates
; caused by the user having dragged the mouse:
CoordMode, Mouse
MouseGetPos, EWR_MouseX, EWR_MouseY
WinGetPos, EWR_WinX, EWR_WinY, EWR_WinW, EWR_WinH, ahk_id %EWR_MouseWin%
SetWinDelay, -1 ; Makes the below move faster/smoother.
if (EWR_MouseX-EWR_WinX) <= (EWR_WinW/3)
{
if (EWR_MouseY-EWR_WinY) <= (EWR_WinH/3)
{
WinMove, ahk_id %EWR_MouseWin%,, EWR_WinX + EWR_MouseX - EWR_MouseStartX, EWR_WinY + EWR_MouseY - EWR_MouseStartY, EWR_WinW + EWR_MouseStartX - EWR_MouseX, EWR_WinH + EWR_MouseStartY - EWR_MouseY
}
else if (EWR_MouseY-EWR_WinY) <= (EWR_WinH/3)*2
{
WinMove, ahk_id %EWR_MouseWin%,, EWR_WinX + EWR_MouseX - EWR_MouseStartX, , EWR_WinW + EWR_MouseStartX - EWR_MouseX,
}
else
{
WinMove, ahk_id %EWR_MouseWin%,, EWR_WinX + EWR_MouseX - EWR_MouseStartX, , EWR_WinW + EWR_MouseStartX - EWR_MouseX, EWR_WinH + EWR_MouseY - EWR_MouseStartY
}
}
else if (EWR_MouseX-EWR_WinX) <= (EWR_WinW/3)*2
{
if (EWR_MouseY-EWR_WinY) <= (EWR_WinH/2)
{
WinMove, ahk_id %EWR_MouseWin%,,, EWR_WinY + EWR_MouseY - EWR_MouseStartY,, EWR_WinH + EWR_MouseStartY - EWR_MouseY
}
else
{
WinMove, ahk_id %EWR_MouseWin%,,,,, EWR_WinH + EWR_MouseY - EWR_MouseStartY
}
}
else
{
if (EWR_MouseY-EWR_WinY) <= (EWR_WinH/3)
{
WinMove, ahk_id %EWR_MouseWin%,,, EWR_WinY + EWR_MouseY - EWR_MouseStartY, EWR_WinW + EWR_MouseX - EWR_MouseStartX, EWR_WinH + EWR_MouseStartY - EWR_MouseY
}
else if (EWR_MouseY-EWR_WinY) <= (EWR_WinH/3)*2
{
WinMove, ahk_id %EWR_MouseWin%,,, , EWR_WinW + EWR_MouseX - EWR_MouseStartX,
}
else
{
WinMove, ahk_id %EWR_MouseWin%,,, , EWR_WinW + EWR_MouseX - EWR_MouseStartX, EWR_WinH + EWR_MouseY - EWR_MouseStartY
}
}
EWR_MouseStartX := EWR_MouseX ; Update for the next timer-call to this subroutine.
EWR_MouseStartY := EWR_MouseY
return

Easy Window Moving and Resizing

Posted: January 29, 2011 by jellymann in computers, hotkeys, keyboard, mouse, windows

Normally, a window can only be dragged by clicking on its title bar. This script extends that so that any point inside a window can be dragged while holding down a modifier, in this case the Windows key. This is a slightly modified version of this script, and I mentioned it in one of my previous AutoHotkey posts.

#LButton::
CoordMode, Mouse ; Switch to screen/absolute coordinates.
MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
WinGetPos, EWD_OriginalPosX, EWD_OriginalPosY,,, ahk_id %EWD_MouseWin%
WinGet, EWD_WinState, MinMax, ahk_id %EWD_MouseWin%
if EWD_WinState = 0 ; Only if the window isn't maximized
SetTimer, EWD_WatchMouse, 10 ; Track the mouse as the user drags it.
return

EWD_WatchMouse:
GetKeyState, EWD_LButtonState, LButton, P
if EWD_LButtonState = U ; Button has been released, so drag is complete.
{
SetTimer, EWD_WatchMouse, off
return
}
GetKeyState, EWD_EscapeState, Escape, P
if EWD_EscapeState = D ; Escape has been pressed, so drag is cancelled.
{
SetTimer, EWD_WatchMouse, off
WinMove, ahk_id %EWD_MouseWin%,, %EWD_OriginalPosX%, %EWD_OriginalPosY%
return
}
; Otherwise, reposition the window to match the change in mouse coordinates
; caused by the user having dragged the mouse:
CoordMode, Mouse
MouseGetPos, EWD_MouseX, EWD_MouseY
WinGetPos, EWD_WinX, EWD_WinY,,, ahk_id %EWD_MouseWin%
SetWinDelay, -1 ; Makes the below move faster/smoother.
WinMove, ahk_id %EWD_MouseWin%,, EWD_WinX + EWD_MouseX - EWD_MouseStartX, EWD_WinY + EWD_MouseY - EWD_MouseStartY
EWD_MouseStartX := EWD_MouseX ; Update for the next timer-call to this subroutine.
EWD_MouseStartY := EWD_MouseY
return

Gnome windows can also be resized using a modifier+middle mouse button. So to have that functionality in windows, I tweaked the above script a little bit. First, since I’m not very familiar with AHK, I studied the script for a few minutes to find out where all the action lies. I discovered the line that does the actual moving based on all the information collected from user input, etc:

WinMove, ahk_id %EWD_MouseWin%,, EWD_WinX + EWD_MouseX - EWD_MouseStartX, EWD_WinY + EWD_MouseY - EWD_MouseStartY

This means: Move the window (WinMove) that’s under the mouse (ahk_id %EWD_MouseWin%) to position (X,Y), which is the original position of the window (EWD_WinX,EWD_WinY) plus the distance moved by the mouse, which is the position of the mouse (EWD_MouseX,EWD_MouseY) subtracted by the position of the mouse before it was moved (EWD_MouseStartX,EWD_MouseStartY).

Now all I had to do was replace that code with new code that resizes the window instead of moving it. The WinMove function above is taking three arguments, the window to be moved, X and Y. To resize a window, all one has to do is add width and height at the end of the function, so it would be something like this:

WinMove, window, x, y, width, height

The tricky thing about resizing is that the window must resize in a different direction depending on what region of the window the mouse is in. The following image shows where these regions are:

Basically, the window is cut into thirds vertically, then the two side regions are cut into thirds again horizontally, and the center is cut in half horizontally. I used a bunch of nested if-statements to check which region the mouse cursor is and then move/resize the window accordingly. I’m not sure if it is the best way, but it works. Here is that part of the script:

if (EWD_MouseX-EWD_WinX) <= (EWD_WinW/3)
{
if (EWD_MouseY-EWD_WinY) <= (EWD_WinH/3)
{
WinMove, ahk_id %EWD_MouseWin%,, EWD_WinX + EWD_MouseX - EWD_MouseStartX, EWD_WinY + EWD_MouseY - EWD_MouseStartY, EWD_WinW + EWD_MouseStartX - EWD_MouseX, EWD_WinH + EWD_MouseStartY - EWD_MouseY
}
else if (EWD_MouseY-EWD_WinY) <= (EWD_WinH/3)*2
{
WinMove, ahk_id %EWD_MouseWin%,, EWD_WinX + EWD_MouseX - EWD_MouseStartX, , EWD_WinW + EWD_MouseStartX - EWD_MouseX,
}
else
{
WinMove, ahk_id %EWD_MouseWin%,, EWD_WinX + EWD_MouseX - EWD_MouseStartX, , EWD_WinW + EWD_MouseStartX - EWD_MouseX, EWD_WinH + EWD_MouseY - EWD_MouseStartY
}
}
else if (EWD_MouseX-EWD_WinX) <= (EWD_WinW/3)*2
{
if (EWD_MouseY-EWD_WinY) <= (EWD_WinH/2)
{
WinMove, ahk_id %EWD_MouseWin%,,, EWD_WinY + EWD_MouseY - EWD_MouseStartY,, EWD_WinH + EWD_MouseStartY - EWD_MouseY
}
else
{
WinMove, ahk_id %EWD_MouseWin%,,,,, EWD_WinH + EWD_MouseY - EWD_MouseStartY
}
}
else
{
if (EWD_MouseY-EWD_WinY) <= (EWD_WinH/3)
{
WinMove, ahk_id %EWD_MouseWin%,,, EWD_WinY + EWD_MouseY - EWD_MouseStartY, EWD_WinW + EWD_MouseX - EWD_MouseStartX, EWD_WinH + EWD_MouseStartY - EWD_MouseY
}
else if (EWD_MouseY-EWD_WinY) <= (EWD_WinH/3)*2
{
WinMove, ahk_id %EWD_MouseWin%,,, , EWD_WinW + EWD_MouseX - EWD_MouseStartX,
}
else
{
WinMove, ahk_id %EWD_MouseWin%,,, , EWD_WinW + EWD_MouseX - EWD_MouseStartX, EWD_WinH + EWD_MouseY - EWD_MouseStartY
}
}

The second thing I changed what what mouse button to use. There are two places where this has to change:

The first line #LButton:: must be changed to #MButton:: to use the middle button for resizing.

Then, a few lines down, after EWD_WatchMouse:, there is the line which checks if the mouse button is down: GetKeyState, EWD_LButtonState, LButton, P. All we do is change LButton to MButton, so it looks like this: GetKeyState, EWD_LButtonState, MButton, P. I didn’t bother with changing EWD_LButtonState because that’s just a variable name, and when I did try change it, and all other instances of it, the script stopped working, so I just left it.

And here is the finished script:
[UPDATE] This script doesn’t work with the Easy Window Move script unless it is separate. My next post has a simple fix for that.

#MButton::
CoordMode, Mouse ; Switch to screen/absolute coordinates.
MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
WinGetPos, EWD_OriginalPosX, EWD_OriginalPosY,,, ahk_id %EWD_MouseWin%
WinGet, EWD_WinState, MinMax, ahk_id %EWD_MouseWin%
if EWD_WinState = 0 ; Only if the window isn't maximized
SetTimer, EWD_WatchMouse, 10 ; Track the mouse as the user drags it.
return

EWD_WatchMouse:
GetKeyState, EWD_LButtonState, MButton, P
if EWD_LButtonState = U ; Button has been released, so drag is complete.
{
SetTimer, EWD_WatchMouse, off
return
}
GetKeyState, EWD_EscapeState, Escape, P
if EWD_EscapeState = D ; Escape has been pressed, so drag is cancelled.
{
SetTimer, EWD_WatchMouse, off
WinMove, ahk_id %EWD_MouseWin%,, %EWD_OriginalPosX%, %EWD_OriginalPosY%
return
}
; Otherwise, reposition the window to match the change in mouse coordinates
; caused by the user having dragged the mouse:
CoordMode, Mouse
MouseGetPos, EWD_MouseX, EWD_MouseY
WinGetPos, EWD_WinX, EWD_WinY, EWD_WinW, EWD_WinH, ahk_id %EWD_MouseWin%
SetWinDelay, -1 ; Makes the below move faster/smoother.
if (EWD_MouseX-EWD_WinX) <= (EWD_WinW/3)
{
if (EWD_MouseY-EWD_WinY) <= (EWD_WinH/3)
{
WinMove, ahk_id %EWD_MouseWin%,, EWD_WinX + EWD_MouseX - EWD_MouseStartX, EWD_WinY + EWD_MouseY - EWD_MouseStartY, EWD_WinW + EWD_MouseStartX - EWD_MouseX, EWD_WinH + EWD_MouseStartY - EWD_MouseY
}
else if (EWD_MouseY-EWD_WinY) <= (EWD_WinH/3)*2
{
WinMove, ahk_id %EWD_MouseWin%,, EWD_WinX + EWD_MouseX - EWD_MouseStartX, , EWD_WinW + EWD_MouseStartX - EWD_MouseX,
}
else
{
WinMove, ahk_id %EWD_MouseWin%,, EWD_WinX + EWD_MouseX - EWD_MouseStartX, , EWD_WinW + EWD_MouseStartX - EWD_MouseX, EWD_WinH + EWD_MouseY - EWD_MouseStartY
}
}
else if (EWD_MouseX-EWD_WinX) <= (EWD_WinW/3)*2
{
if (EWD_MouseY-EWD_WinY) <= (EWD_WinH/2)
{
WinMove, ahk_id %EWD_MouseWin%,,, EWD_WinY + EWD_MouseY - EWD_MouseStartY,, EWD_WinH + EWD_MouseStartY - EWD_MouseY
}
else
{
WinMove, ahk_id %EWD_MouseWin%,,,,, EWD_WinH + EWD_MouseY - EWD_MouseStartY
}
}
else
{
if (EWD_MouseY-EWD_WinY) <= (EWD_WinH/3)
{
WinMove, ahk_id %EWD_MouseWin%,,, EWD_WinY + EWD_MouseY - EWD_MouseStartY, EWD_WinW + EWD_MouseX - EWD_MouseStartX, EWD_WinH + EWD_MouseStartY - EWD_MouseY
}
else if (EWD_MouseY-EWD_WinY) <= (EWD_WinH/3)*2
{
WinMove, ahk_id %EWD_MouseWin%,,, , EWD_WinW + EWD_MouseX - EWD_MouseStartX,
}
else
{
WinMove, ahk_id %EWD_MouseWin%,,, , EWD_WinW + EWD_MouseX - EWD_MouseStartX, EWD_WinH + EWD_MouseY - EWD_MouseStartY
}
}
EWD_MouseStartX := EWD_MouseX ; Update for the next timer-call to this subroutine.
EWD_MouseStartY := EWD_MouseY
return

My Escape key stopped working today. After freaking out for a few minutes about where I’m going to get the money to buy a new keyboard, I made a quick AHK script that remaps the Caps Lock key to Escape. Then I realised that I would probably tap it by accident doing some irreversible damage while typing, so I thought I’d remap Scroll Lock to Escape (Who uses scroll lock, anyway?):

ScrollLock::
{
Send {Esc}
return
}

On the topic of Caps Lock, everyone know how annoying it is when you accidentally press it and your wall post’s mood changes significantly. SERIOUSLY ANNOYING. So here is how to remap the Caps Lock key to something else. In this case, it acts as a third Control key:

CapsLock::Ctrl

If you sometimes do need to use the Caps Lock, like when you’re flaming a forum, or coding a serious error message, you can map Shift+Caps Lock to toggle Caps Lock. I tried doing it like this, which is the way most people did it in the foums:

+CapsLock::CapsLock

But unfortunately that only turned Caps Lock on and not off. And since my Caps Lock key is remapped to Control, I couldn’t turn it off without killing the AHK script.

The following script does work, as it checks the state of Caps Lock and then acts accordingly:

+CapsLock::
{
if GetKeyState("Capslock", "T")
SetCapsLockState, Off
else
SetCapsLockState, On
return
}

I’ve always wanted my scroll wheel to be an easy volume-control, as my keyboard does not have media keys. I Googled and went through forum after forum to find out what AHK commands control volume, but none of them worked right. Then today I stumbled across something. The media keys are keyboard keys after all, and can be mapped and remapped in AHK just like any other, even if your keyboard doesn’t have them. Here is a simple script that uses the scroll wheel and middle mouse button to change and mute the volume:

#MButton::Volume_Mute
#WheelDown::Volume_Down
#WheelUp::Volume_Up

When in doubt, consult the AutoHotkey Key List.
I also recommend you also take a look at my other post on AutoHotkey.

AutoHotkey puts the HOT in Hotkey

Posted: November 25, 2010 by jellymann in computers, hotkeys, keyboard, mouse, tech, windows

If you’re a Windows user and you don’t know about AutoHotkey, you’re living a sad, sad life.

AutoHotkey unleashes the full potential of your keyboard, joystick, and mouse. For example, in addition to the typical Control, Alt, and Shift modifiers, you can use the Windows key and the Capslock key as modifiers. In fact, you can make any key or mouse button act as a modifier.”

AutoHotkey (AHK) works with scripts. The basic syntax is fairly easy to understand, however some of the more complicated stuff is, well, more complicated.

Lets look at an example of a simple but very useful AHK script; a hotkey that runs a program, eg. Calculator, when WindowsKey and C is pressed:

#c:: Run, calc

The hash (#) represents the Windows Key. The letter after that is the key to be pressed with the Windows Key. Thus ‘#c‘ means the hotkey is activated when the WindowsKey and C is pressed. Other modifiers include Shift, represented by a plus sign (+), Control by a caret (^) and Alt by an exclamation mark (!).

Then there’s two colons (::) directly after defining the keys. Whatever is on the right of the colons is what is executed when the keys are pressed.

Run‘ quite simply runs the command after the comma, and ‘calc‘ is the command in Windows for opening the Calculator. You can do the same thing with ‘notepad‘, ‘cmd‘, ‘iexplore‘, and loads more.

Scripts can obviously be more than one line long, for example:

#c::
Sleep 10
Run, calc
return

Does basically the same thing, except for the ‘Sleep‘ command, which, in this case, waits ten milliseconds before running ‘calc‘.

The ‘return‘ function ends the multi-line hotkey. If you didn’t have this here and you define another hotkey after that one, it might think that hotkey is part of the previous one.

Besides built-in Windows programs like ‘calc‘ and ‘notepad‘, other programs can be ‘Run’. For example, I use Notepad++ and use Windows Key and N to launch it using the following script:

#n::
Sleep 10
Run, E:\programs\npp\notepad++.exe
return

You can even ‘Run‘ websites. For example, you could make Windows Key and F open Facebook in your default browser:

#f::
Sleep 10
Run, http://www.facebook.com
return

I have a very useful hotkey that Googles whatever I select by pressing Windows Key and G:

#g::
Send, ^c
Sleep 50
Run, http://www.google.com/search?q=%clipboard%
return

The ‘Send‘ command here “sends” Control+C to the computer as though you pressed it on the keyboard. This copies whatever text you might have selected into the clipboard.

After sleeping for fifty milliseconds (to make sure the computer is finished copying your selection) it runs Google with the copied text as the search criteria.

%clipboard%’ puts whatever is copied into the clipboard wherever you want. You can even put it in a ‘Send’ command (i.e. Send, %clipboard%)

Scripts can get very complicated, for instance, this script I have which allows me to move my Windows around without dragging from the titlebar by holding down the Windows Key (like Gnome/Ubuntu):

#LButton::
CoordMode, Mouse ; Switch to screen/absolute coordinates.
MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
WinGetPos, EWD_OriginalPosX, EWD_OriginalPosY,,, ahk_id %EWD_MouseWin%
WinGet, EWD_WinState, MinMax, ahk_id %EWD_MouseWin%
if EWD_WinState = 0 ; Only if the window isn’t maximized
SetTimer, EWD_WatchMouse, 10 ; Track the mouse as the user drags it.
return

EWD_WatchMouse:
GetKeyState, EWD_LButtonState, LButton, P
if EWD_LButtonState = U ; Button has been released, so drag is complete.
{
SetTimer, EWD_WatchMouse, off
return
}
GetKeyState, EWD_EscapeState, Escape, P
if EWD_EscapeState = D ; Escape has been pressed, so drag is cancelled.
{
SetTimer, EWD_WatchMouse, off
WinMove, ahk_id %EWD_MouseWin%,, %EWD_OriginalPosX%, %EWD_OriginalPosY%
return
}
; Otherwise, reposition the window to match the change in mouse coordinates
; caused by the user having dragged the mouse:
CoordMode, Mouse
MouseGetPos, EWD_MouseX, EWD_MouseY
WinGetPos, EWD_WinX, EWD_WinY,,, ahk_id %EWD_MouseWin%
SetWinDelay, -1 ; Makes the below move faster/smoother.
WinMove, ahk_id %EWD_MouseWin%,, EWD_WinX + EWD_MouseX – EWD_MouseStartX, EWD_WinY + EWD_MouseY – EWD_MouseStartY
EWD_MouseStartX := EWD_MouseX ; Update for the next timer-call to this subroutine.
EWD_MouseStartY := EWD_MouseY
return

Don’t ask me to explain this, I got it from somewhere else. By the way, anything after a semi-colon (;) is a comment and is ignored by AHK.

Here are a few more that I use:

Windows Key and H toggles Show/Hide Hidden Files in Explorer:

#h::
RegRead, HiddenFiles_Status, HKEY_CURRENT_USER, Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced, Hidden
If HiddenFiles_Status = 2
RegWrite, REG_DWORD, HKEY_CURRENT_USER, Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced, Hidden, 1
Else
RegWrite, REG_DWORD, HKEY_CURRENT_USER, Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced, Hidden, 2
WinGetClass, eh_Class,A
If (eh_Class = “#32770” OR A_OSVersion = “WIN_VISTA”)
send, {F5}
Else PostMessage, 0x111, 28931,,, A
Return

Windows Key and Right Mouse Button (Yes you can combine with mouse events!) minimizes the Window under the mouse cursor:

#RButton::
MouseGetPos, , , WinID, control
WinGetPos, WinX, WinY, WinWidth, , ahk_id %WinID%
WinMinimize, ahk_id %WinID%
return

Windows Key and Q is a lot more comfortable than Alt F4, which is a commonly used hotkey for me. This simple one-liner saves me some wrist aerobics:

#Q::WinClose,A

Check out the Tutorial on AHK scripting, the list of keys, or the list of commands that you can use.

I hope you find these scripts useful. Also, I’d love to see what scripts you guys are using, too.

Special thanks to @EttVenter for telling me about the awesomeness of AutoHotkey, and @brskln, who’s recent tweets inspired this blog post.

Read my other post for a few more scripts to make your life easier.