Iris Classon
Iris Classon - In Love with Code

Creating a dynamic hotkey window overlay with AutoHotKey

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.

 

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

 

The function does the following:

 

And this is how the text size is calculated:

 

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

 

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

 

 

Comments

Leave a comment below, or by email.


Last modified on 2015-01-30

comments powered by Disqus