Comparing Today’s AutoHotkey Version 1.1 and the Future Version 2.0 (Part 5—Replacing V1.1 gLabels with V2.0 GuiControl.OnEvent())

AutoHotkey Version 2.0 Drops the GUI gLabel in Favor of the Object OnEvent() Function

In AutoHotkey V1.1, the primary method for adding action to GUI pop-up windows employs the gLabel inserted into a GUI control’s options. As AutoHotkey moves to object-oriented programming in V2.0, the Gui.OnEvent() function replaces gLabels.

Launch Window V2In AutoHotkey V2.0, each GUI control responds to different Gui Events. For example, with the Gui Button control, you can register OnEvent() functions for Click, DoubleClick, Focus, and LoseFocus, while the Edit control directly supports Change. You register each type of initiating action you use with the OnEvent() function. In fact, you must register an event before AutoHotkey will respond.

Replacing a gLabel in the AutoHotkey V1.1 GUI Picture control in the LaunchWindow.ahk script:

Gui, Add, Picture, w30 h-1 section gLaunchGoogle Icon1
   , C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

yields a more complex OnEvent() object code in V2.0:

LaunchWindow := GuiCreate()
LaunchWindow.Add("Picture","w30 Icon1"
    ,"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
    .OnEvent("Click","LaunchGoogle")

(AutoHotkey reads each of the Gui Add lines of code above as one continuous line using line continuation techniques.)

The V1.1 gLaunchGoogle GUI subroutine turns into the LaunchGoogle() function using the V2.0  OnEvent(“Click”,”LaunchGoogle”). In the above example, I appended the OnEvent() function to the Gui.Add() function with a single dot. However, best practices may suggest saving each control as a unique object by assigning the Gui.Add() function to an object variable. This makes it easier to add other events to the object—although requiring a little more code:

LaunchWindow := GuiCreate()
GoogleIcon := LaunchWindow.Add("Picture","w30 Icon1"
     ,"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
GoogleIcon.OnEvent("Click","LaunchGoogle")

Now, you can change or execute more properties and methods my merely calling the control object by name:

GoogleIcon.Opt("Hidden")  ; Hides the picture control

The Hidden option hides the picture control GoogleIcon. You can later display the icon with:

GoogleIcon.Visible

Converting gLabels to V2.0 OnEvent Functions

Library BenefitsThis conversion of gLabels from AutoHotkey V1.1 to V2.0 presents the most complicated translation I’ve encountered (so far). While I might be able to drum up some type of Regex to help in the translation, even then, I would need to account for the different events available in the various GUI controls. Each time a V1.1 Gui Add statement includes the gLabel option, you must write at least one corresponding OnEvent() function for V2.0. In most cases, I see myself doing it manually—although, I might use the InstantHotkey.ahk app to insert the OnEvent() syntax. (Or, if I do it enough, I could add a text replacement to my AutoCorrect script.)

The A_GuiEvent and A_GuiControlEvent variables—which we may have used in V1.1 to detect events (other than the default)—disappear in V2.0. A GUI control in V1.1 only allows one gLabel. (While multiple gLabels may coexist within a control’s options, only the last one goes into effect.) You must capture other events with one of those two variables to initiate alternative actions. In AutoHotkey V2.0, if you want to activate additional actions with other GUI control events, then you simply register each by adding a separate GUI OnEvent() function.

You can use OnEvent() in V2.0 to prioritize the events by adding a third parameter (from the online documentation): “1 (the default): Call the callback after any previously registered callbacks; -1 Call the callback before any previously registered callbacks; 0: Do not call the callback.”

The entire LaunchWindow.ahk script rewritten for AutoHotkey V2.0 appears as follows.

LaunchWindow := GuiCreate()

LaunchWindow.SetFont("s12 cBlue", "Arial")
LaunchWindow.Title := "Launch Program"
MyGoogle := LaunchWindow.Add("Picture","w30 Icon1"
  ,"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
MyGoogle.OnEvent("Click","LaunchGoogle")
MyGoogleButton := LaunchWindow.Add("Button","ys","Google")
MyGoogleButton.OnEvent("Click","LaunchGoogle")

MyFirefox := LaunchWindow.Add("Picture","w30 section xs Icon1"
  ,"C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
MyFirefox.OnEvent("Click","LaunchFirefox")
MyFirefoxButton := LaunchWindow.Add("Button","ys","Firefox")
MyFirefoxButton.OnEvent("Click","LaunchFirefox")

MyEdge := LaunchWindow.Add("Picture","w30 section xs Icon1"
 ,"C:\Windows\SystemApps\Microsoft
 .MicrosoftEdge_8wekyb3d8bbwe\MicrosoftEdge.exe")
MyEdge.OnEvent("Click","LaunchEdge")
MyEdgeButton := LaunchWindow.Add("Button","ys","Microsoft Edge")
MyEdgeButton.OnEvent("Click","LaunchEdge")

MyDropBox := LaunchWindow.Add("Picture","w30 section xs Icon1"
  ,"C:\Users\" . A_UserName . "\AppData\Roaming\Dropbox\bin\Dropbox.exe")
MyDropBox.OnEvent("Click","LaunchDropBox")
MyDropBoxButton := LaunchWindow.Add("Button","ys","DropBox")
MyDropBoxButton.OnEvent("Click","LaunchDropBox")

LaunchWindow.Show()
Return

LaunchGoogle()
{
 Run "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
 WinClose
}

LaunchFirefox()
{
 Run "C:\Program Files (x86)\Mozilla Firefox\firefox.exe www.facebook.com"
 WinClose
}

LaunchEdge()
{
 Run "C:\Windows\explorer.exe shell:Appsfolder\Microsoft
.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge"
 WinClose
}

LaunchDropBox()
{
 Run "C:\Users\" . A_UserName . "\Dropbox"
 WinClose
}

While the script grows longer with the addition of all of the GUI OnEvent() statements, V2.0 appears to give you more direct access to each GUI control.

Translating the gLabel in the WindowMove.ahk Script

MoveWindow DropDownListI translated the V1.1 WindowMove.ahk script into a V2.0 WindowMove.ahk2 script. (You can view the script at the ComputorEdge Free AutoHotkey Scripts page.) I won’t pretend that it didn’t take a little time. Even though a fairly short script, with the exception of the formulas, virtually every line of code needed rewriting as V2.0 functions. The non-standard positioning of the V1.1 WinMove command parameters added to the problem. (I’ll discuss more about that in the next blog.)

The V1.1 gLabel option gPosChoice gets removed from the Gui, Add options and converts into the V2.0 OnEvent() function:

WinDropDown.OnEvent("Change","PosChoice")

(The script assigned WinDropDown as the DropDownList control object name using the Gui.add() function.)

Since many of the V1.1 %var% replacements appear in new functions as expressions, I merely needed to remove the percent signs % with a standard search and replace—no quotation marks needed (or desired). That gave me more insight into how the new V2.0 functions can make adding certain features easier.

One of the original V1.1 WinMove commands which center the target window appears as follows:

WinMove,%WinName%,,%X4%,%Y4%,%X2%,%Y2%

This converts to the V2.0 code:

WinMove X4,Y4,X2,Y2, WinName

(Note that WinName moves to the end of the line.)

Ignoring the change in their order, each parameter represents a variable expression. That makes modifying size and location options easier. We can now add calculations directly to the expression.

For example, if I want to add a new option to display a larger window in the center of the screen, then, after adding the new item to the DropDownList menu, I can insert the calculations directly into the WinMove function:

WinMove X4/2,Y4/2,X2*1.5,Y2*1.5, WinName

I change the location of the upper-left corner of the window by dividing the original coordinates by two (e.g. X4/2, Y4/2). I multiply by 1.5 to increase the dimensions of the window by 50% (e.g. X2*1.5, Y2*1.5). Adding this new option in AutoHotkey V1.1 (using %var%) would come with a little more complexity.

Next time, I highlight a couple of V1.1 commands which use non-standard syntax—making the conversion to V2.0 a little more complicated.

jack

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s