Jan 302015
 
 January 30, 2015  Posted by at 9:05 am AutoHotKey, Windows  Add comments

I was working on some AutoHotKey (my favorite desktop automation tool) videos and I asked myself how I best could showcase the keyboard shortcuts that I was using in the scripts. I could always add a layer in the video while editing the video, but that would mean a lot of repetitive work- and tedious. Pretty much what I use AHK for, to avoid repetitive work.

dynamic hotkey window with autohotkey

 

So instead I created a script that would create a window that would overlay my screen but not demand focus for keyboard and mouse input. The placement of the window is calculated based of the monitor resolution (in this script assuming only one monitor- I’ll update it to deal with several monitors next week), and the window size is set to calculated input string (which is the hotkey combo passed into the function as a string). The window is displayed for 5 seconds then destroyed, releasing the memory as I don’t need to keep it around and therefore I prefer to recreate it instead when needed. Here is the script which might need some refactoring 🙂

Whenever I want to display the window I call the CreateWindow function passing in the keys

dynamic hotkey window with autohotkey 1

 

The function does the following:

dynamic hotkey window with autohotkey 2

 

And this is how the text size is calculated:

dynamic hotkey window with autohotkey 3

 

The SetTimer function in the CreateWindow function calls the RemoveGui function after 5 seconds, and that function just calls destroy on the GUI

dynamic hotkey window with autohotkey 4

 

Here is the full script. I don’t have syntax highlighting support for AHK on the blog, so pardon for just adding it as text:

CreateWindow(key){
GetTextSize(key,35,Verdana,height,width)
bgTopPadding = 40
bgWidthPadding = 100
bgHeight = % height + bgTopPadding
bgWidth = % width + bgWidthPadding
padding = 20
yPlacement = % A_ScreenHeight – bgHeight – padding
xPlacement = % A_ScreenWidth – bgWidth – padding

Gui, Color, 46bfec
Gui, Margin, 0, 0
Gui, Add, Picture, x0 y0 w%bgWidth% h%bgHeight%, C:\Users\IrisDaniela\Pictures\bg.png
Gui, +LastFound +AlwaysOnTop -Border -SysMenu +Owner -Caption +ToolWindow
Gui, Font, s35 cWhite, Verdana
Gui, Add, Text, xm y20 x25 ,%key%
Gui, Show, x%xPlacement% y%yPlacement%
SetTimer, RemoveGui, 5000
}

GetTextSize(str, size, font,ByRef height,ByRef width) {
Gui temp: Font, s%size%, %font%
Gui temp:Add, Text, , %str%
GuiControlGet T, temp:Pos, Static1
Gui temp:Destroy
height = % TH
width = % TW
}

RemoveGui:
Gui, Destroy
return

#7::
CreateWindow(“Win + 7”)
return

 

 

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

What is 15 + 8 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)