Classes in AHK, Basic tutorial.

Helpful script writing tricks and HowTo's
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

Classes in AHK, Basic tutorial.

20 Jan 2015, 12:24

Hello everybody,
I spend a couple of days trying to understand how Classes in AHK works.
Thanks to a forum community I manage to get basic representation of it.

Unfortunately in Help file Classes described in very complicated way without examples.
So I decided to make basic tutorial for beginners. Looks like it is big Tutorial and I could make mistakes so if you find them pleas let me know and I'll fix them.


We will discuss, Classes, Instances, Variables, Methods, Nested Classes, Class Inheritance, __New, __Delete.

Using Classes in programming is Calling Object Oriented Programming (O.O.P.) hope you here about it.
In Classes there are two main definitions:
Class
Instance

So what is class?
Imagine that you want to build a Car. To make it you have to have Plan of it.
This plane is Class. In AHK it looks like this:

Code: Select all

Class ClassName{
}
So what is Instance?
Imagine that you build Car using Plan, and this Car is Instance.
So Plan is Class and Car is Instance. In AHK creating Instance from Class looks like this:

Code: Select all

InstanceName:= New ClassName
Every Class has Variables and Methods.

So what is Class Variables?
Imagine you car has name for example BMW - this is property and it looks like - CarName:=BMW.
Also it has 4 wheels it is also property. For example WheelsAmount:=4

So what is Class Method?
Imagine when turn the steering wheel the car turns - this is method, and it looks like - TurnSteerWheel().
Also when you use key to open car this is also method for example CarOpenKey().

So imagine you made plane where all that Variables and Methods was defined, now you want to build a car. This is how it looks in AHK:

Code: Select all

;First we have to declare class:
Class Car{
	CarName:="BMW"
	WheelsAmount:=4
	TurnSteerWheel()
	{
		Some Commands here...
	}
	CarOpenKey(){
		Some Commands here...
	}
}
; Then create Instane of that class with name BMWConcept
BMWConcept:=New Car
This was very basics, so lets make something with Classes in AHK.
Lets make simple Class of Window. Window has a lot of properties like Windows Title, Windows ID, Position, Size, e.t.c. these will be Class Variables.
Also we can make with window a lot of actions for example Activate it, Get its Title, Get its ID, Get it's position e.t.c. Actions like this will be our Class Methods.
So lets make simple Class for Notepad of one Variable and one Method.

Code: Select all

#SingleInstance force

;Declare Class
Class Window{
	Title:= "Untitled - Notepad" ;Variable Title
	Activate() ;Activates window with Title - This.Title
	{ 
		IfWinExist, % This.Title
			WinActivate 
		else
			MsgBox % "There is no Window: "This.Title "`nPleas Run Notepad!"

	}  	
}

;Create Instance
SomeWin:= New Window
Return

!^a:: ;Hotkey to run Instance Method
SomeWin.Activate()
Return

So now lets make method that will automatically get for us Windows properties and also add some more methods:

Code: Select all

#SingleInstance force

;Declare Class
Class Window{
	
	Get()
	{
		WingetTitle TitleVar, A ; Get title from Active window.
		This.Title:=TitleVar ; Set TitleVar to This.Title
		
		WinGet IDVar,ID,A ; Get ID from Active window.
		This.ID:=IDVar ; Set IDVar to This.ID
	}
	Activate() ;Activates window with Title - This.Title
	{ 
		;MsgBox % This.ID
		WinActivate % "ahk_id "This.ID ;Use word "This" when you reffer to variable of this Class.
		Return This.ID
	}   
	AnnounceWinProps() ;Create message box with title and ID
	{
		MsgBox % "Title is: " This.Title "`n ID is: " This.ID
	}
}
;Create Instance
SomeWin:= New Window
return

^!a:: ;Hotkey to run Instance Method

IfWinExist, ahk_class Notepad
	WinActivate ahk_class Notepad
else
{
	MsgBox % "There is no Window: Notepad.`nPleas Run Notepad!"
	exit
}

SomeWin.get()
SomeWin.AnnounceWinProps()
Return

^!s::
If SomeWin.ID
	SomeWin.Activate() 
else
	MsgBox Instance SomeWindow has no ID setted, pleas run SomeWin.get() first!
return

So what was added?

1. Was added methot - Get(). This method retrieves Window Title and Window ID from Active window.
2. Variable title was removed, because we will use method Get() to get Title.
2. Method Activate was modified and now it use not Window Title but Window ID.
3. Method AnnounceWinProps() was added, that show us Title and ID that was retrieved by Get()
4. Hotkey ^!a was modified now it activates Notepad, than run Get() Method.
5. Hotkey ^!s was added. This hotkey activates Window with Method Activate() if ID is True(was retrieved).

Now lets add nested Class to our Class Window with couple of variables. So what is Nested Class?
Nested Class is just Class inside another Class and you can access it like this BaseClass.NestedClass To acces variable from Nested class use this: BaseClass.NestedClass.Var or Method - BaseClass.NestedClass.Method().

Code: Select all

#SingleInstance force

;Declare Class
Class Window{
	
	Class Tmp{
		A:=1
		Static B:=1
	}
	
	Get()
	{
		WingetTitle TitleVar, A ; Get title from Active window.
		This.Title:=TitleVar ; Set TitleVar to This.Title
		
		WinGet IDVar,ID,A ; Get ID from Active window.
		This.ID:=IDVar ; Set IDVar to This.ID
	}
	Activate() ;Activates window with Title - This.Title
	{ 
		IfWinExist, % "ahk_id "This.ID
			WinActivate % "ahk_id " This.ID
		else
			MsgBox % "There is no Window with ID: "This.ID
	}  
	AnnounceWinProps() ;Create message box with title and ID
	{
		MsgBox % "Title is: " This.Title "`n ID is: " This.ID
	}
}
;Create Instance
SomeWin:= New Window
return

^!a:: ;Hotkey to run Instance Method
WinActivate ahk_class Notepad
SomeWin.get()
SomeWin.AnnounceWinProps()
Return

^!s::
If SomeWin.ID
	SomeWin.Activate() 
else
	MsgBox Instance SomeWindow has no ID setted, pleas run SomeWin.get() first!
return

^!d::
MsgBox % "SomeWin.Tmp.A = "SomeWin.Tmp.A "`nSomeWin.Tmp.B = "SomeWin.Tmp.B
return


Now if you press "^!d", you will see message where SomeWin.Tmp.A = Nothing. Why?
Lets look how our variables was declared:

Code: Select all

Class Tmp{
		A:=1
		Static B:=1
	}
As you can see A is declared like simple variable and this kind of variable has name - Instance Variable. Instance variables assigns to instance only when they was created. We do bot create Instance from Window.Tmp, but if we decide to do it it will look like this: SomeWin1:= New Window.Tmp. After we created instance this Variable A will be available here: SomeWin1.A
Let's move to variable B, it has word "Static" this kind of variables calls - Static Variables. This variables belong to Class itself, so that is why we could access it, and our message showed us that B:=1.

Now let's talk about Class Inheritance.
So what is it?
Imagine you have Class A and it has Var1 and Var2. You created Class B and you want this Class to derive everything from A. Another words you want Class B have the same variables and Methods that has Class A.
Let's make it:

Code: Select all

Class A{
	Static Var1:=1
	Static Var2:=2
}

Class B extends A{
	Var3:=3
}

NewClass:= New B
MsgBox % "Var1=" NewClass.Var1 "`nVar2=" NewClass.Var2 "`nVar3=" NewClass.Var3
return
As you can see Class B Inherit everything from Class A and add their own Instance Variable Var3.
This is calling Class Inheritance.

Let's back to our Notepad project. Let's create new Class Notepad and make it inherit everything from Class Window and add to it some new Methods.

Code: Select all

#SingleInstance force

;Declare Class
Class Window{
	
	Class Tmp{
		A:=1
		Static B:=1
	}
	
	Get()
	{
		WingetTitle TitleVar, A ; Get title from Active window.
		This.Title:=TitleVar ; Set TitleVar to This.Title
		
		WinGet IDVar,ID,A ; Get ID from Active window.
		This.ID:=IDVar ; Set IDVar to This.ID
	}
	Activate() ;Activates window with Title - This.Title
	{ 
		IfWinExist, % "ahk_id "This.ID
			WinActivate % "ahk_id " This.ID
		else
			MsgBox % "There is no Window with ID: "This.ID
	}  
	AnnounceWinProps() ;Create message box with title and ID
	{
		MsgBox % "Title is: " This.Title "`n ID is: " This.ID
	}
}

Class Notepad extends Window{
	
	Run()
	{
		IfWinNotExist ahk_class Notepad
			Run Notepad
		Else 
			WinActivate
	}	
}

;Create Instance
SomeWin:= New Window
SomeNot:= New Notepad
return

^!a:: ;Hotkey to run Instance Method
SomeNot.Run()
SomeNot.get()
SomeNot.AnnounceWinProps()
Return

^!s::
If SomeWin.ID
	SomeWin.Activate() 
else
	MsgBox Instance SomeWindow has no ID setted, pleas run SomeWin.get() first!
return

^!d::
MsgBox % "SomeWin.Tmp.A = "SomeWin.Tmp.A "`nSomeWin.Tmp.B = "SomeWin.Tmp.B
return

As you can see we add New Class Notepad that extends Window Class and create Instance - SomeNot.
Also take a look at Hotkey - ^!a. As you can see Instanse SomeNote use all inherited methods from Class Window.

And last...
Let's imagine that we want some actions occurs when we create Instance. To initialize that actions we will use: __New, this is how it look like:

Code: Select all

Class A{
	__New(){
		Some commands here...	
	}
}
So let's back to our Script, this is how it looks now check Notepad Class:

Code: Select all

#SingleInstance force

;Declare Class
Class Window{
	
	
	Class Tmp{
		A:=1
		Static B:=1
	}
	
	Get()
	{
		WingetTitle TitleVar, A ; Get title from Active window.
		This.Title:=TitleVar ; Set TitleVar to This.Title
		
		WinGet IDVar,ID,A ; Get ID from Active window.
		This.ID:=IDVar ; Set IDVar to This.ID
	}
	Activate() ;Activates window with Title - This.Title
	{ 
		IfWinExist, % "ahk_id "This.ID
			WinActivate % "ahk_id " This.ID
		else
			MsgBox % "There is no Window with ID: "This.ID
	}  
	AnnounceWinProps() ;Create message box with title and ID
	{
		MsgBox % "Title is: " This.Title "`n ID is: " This.ID
	}
}

Class Notepad extends Window{
	
	__New()
	{	
		This.Run()
		This.get()
	}
	
	Run()
	{
		IfWinNotExist ahk_class Notepad
			Run Notepad
		Else 
			WinActivate
	}	

}
;Create Instance
SomeWin:= New Window
SomeNot:= New Notepad
return

^!a:: ;Hotkey to run Instance Method
;SomeNot.Run()
;SomeNot.get()
;SomeNot.AnnounceWinProps()
Return

^!s::
If SomeNot.ID
	SomeNot.Activate() 
else
	MsgBox Instance SomeWindow has no ID setted, pleas run SomeWin.get() first!
return

^!d::
MsgBox % "SomeWin.Tmp.A = "SomeWin.Tmp.A "`nSomeWin.Tmp.B = "SomeWin.Tmp.B
return
As you can see __New was added in Notepad Class. In this section When we create Instance SomeNot it execute method Run() and Get() now we do not need to run them from hotkey, look at hotkey ^!a::, we do not need those action any more. Also now we can use ^!s to activate Notepad.

Also there is __Del like __New it works when Instance deletes but now I'm not sure how it works, I'll back to it later.

So lets make final touch, lets create Class Calc that will inherit Class Window also we will add to it method Run()

Code: Select all

#SingleInstance force

;Declare Class
Class Window{
	
	
	Class Tmp{
		A:=1
		Static B:=1
	}
	
	Get()
	{
		WingetTitle TitleVar, A ; Get title from Active window.
		This.Title:=TitleVar ; Set TitleVar to This.Title
		
		WinGet IDVar,ID,A ; Get ID from Active window.
		This.ID:=IDVar ; Set IDVar to This.ID
	}
	Activate() ;Activates window with Title - This.Title
	{ 
		IfWinExist, % "ahk_id "This.ID
			WinActivate % "ahk_id " This.ID
		else
			MsgBox % "There is no Window with ID: "This.ID
	}  
	AnnounceWinProps() ;Create message box with title and ID
	{
		MsgBox % "Title is: " This.Title "`n ID is: " This.ID
	}
}

Class Notepad extends Window{
	
	__New()
	{	
		This.Run()
		This.get()
	}
	
	Run()
	{
		IfWinNotExist ahk_class Notepad
			Run Notepad
		Else 
			WinActivate
	}	
	
}

Class Calc extends Window{
	
	__New()
	{	
		This.Run()
		This.get()
	}
	
	Run()
	{
		IfWinNotExist ahk_class CalcFrame
			Run Calc
		Else 
			WinActivate
	}	
	
}
;Create Instance
SomeWin:= New Window
SomeNot:= New Notepad
SomeCalc:= New Calc
return

^!a:: ;Hotkey to run Instance Method
;SomeNot.Run()
;SomeNot.get()
;SomeNot.AnnounceWinProps()
Return

^!s::
If SomeNot.ID
	SomeNot.Activate() 
else
	MsgBox Instance SomeWindow has no ID setted, pleas run SomeWin.get() first!
return

^!d::
If SomeCalc.ID
	SomeCalc.Activate() 
else
	MsgBox Instance SomeWindow has no ID setted, pleas run SomeWin.get() first!
return
As you can see we add method Run() so now it starts Calculator when we create Instance SomeCalc.

So this was basics that I hope will allow you to work with AHK Classes.
User avatar
jethrow
Posts: 188
Joined: 30 Sep 2013, 19:52
Location: Iowa

Re: Classes in AHK, Basic tutorial.

20 Jan 2015, 14:01

samardac wrote:So what is class?
Imagine that you want to build a Car. To make it you have to have Plan of it.
This plane is Class. In AHK it looks like this:
...
So what is Instance?
Imagine that you build Car using Plan, and this Car is Instance.
So Plan is Class and Car is Instance. In AHK creating Instance from Class looks like this:
...
Every Class has Variables and Methods.
It's good to see someone creating some basic tutorials on objects. Honestly, I didn't examine the entire post, but it appears your teaching is targeted at the noobie crowd.
I wanted to make sure it is understood that a class is not only a plan (or blueprint) for an Instance, but the class is an object itself. See Keyword Prototype instead of Class for more discussion.

Code: Select all

class MyClass {
	SayHello() {
		MsgBox Hello
	}
}

MyClass.SayHello()
Also, in your first code example, ClassName is a valid class that doesn't have any Variables nor Methods.
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

Re: Classes in AHK, Basic tutorial.

20 Jan 2015, 15:35

jethrow,
Yep it's for noobie, I found that in Help File explanation is complicated, but people have to start from something.
Class is not only a plan (or blueprint) for an Instance, but the class is an object itself
Thanx for thise remark.
I preffer that beginers think about Classes as blueprint for better understanding at the begining. When they becom familiar with classes they can follow your link for fother studing.
Also, in your first code example, ClassName is a valid class that doesn't have any Variables nor Methods.
Yes I just wanted to show how declare Class in AHK.
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: Classes in AHK, Basic tutorial.

21 Jan 2015, 03:21

Thanks!
Everything is possible!
User avatar
ahkDustVorteX
Posts: 47
Joined: 14 May 2014, 12:08

Re: Classes in AHK, Basic tutorial.

30 Jan 2015, 14:01

Yep it's for noobie, I found that in Help File explanation is complicated, but people have to start from something.
This is the tutorial I needed! Thanks so much samardac! Great job! Very well explained! :superhappy:
Editor: Notepad++
"Those who wait and persist, always achieve."
Guest

Re: Classes in AHK, Basic tutorial.

02 May 2015, 05:44

This post has been very helpful... thank you!
-_+
Posts: 70
Joined: 06 Dec 2014, 12:43

Re: Classes in AHK, Basic tutorial.

08 Oct 2015, 15:37

Poor English is so poor :(
User avatar
niczoom
Posts: 78
Joined: 09 Mar 2016, 22:17

Re: Classes in AHK, Basic tutorial.

20 Jun 2016, 09:22

Thanks for the time and effort.

Now I need to figure out what/where/why I need to use a class for ...!!!
[AHK] 1.1.23.05 x32 Unicode
[WIN] 10 Pro x64 Version 5111 (Build 10586.218)
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Classes in AHK, Basic tutorial.

20 Jun 2016, 14:51

niczoom wrote:Thanks for the time and effort.

Now I need to figure out what/where/why I need to use a class for ...!!!
A lot of libraries here in the forum are classes you could try one of them and compare it to other types of programming to achieve that :)
Recommends AHK Studio
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: Classes in AHK, Basic tutorial.

28 Jun 2016, 07:22

Whilst I agree with Jethrow's comment that a class is not just a blueprint, I also agree with samardac that it would needlessly confuse newbies:

Code: Select all

msgbox % "var1: " MyClass.var1
msgbox % "var2: " MyClass.var2
msgbox % "var3: " MyClass.var3

Class MyClass {
   var1 := "One"
   static var2 := "Two"

   __New(){
      this.var3 := "Three"
   }
}
For the scope of this tutorial, I think code like the above would only confuse them.
User avatar
megnatar
Posts: 92
Joined: 27 Oct 2014, 20:49
Location: The Netherlands
Contact:

Re: Classes in AHK, Basic tutorial.

29 Sep 2016, 05:10

Thanks for the tutorial!!!

And good comments . . ..
Sarusei
Posts: 3
Joined: 28 Mar 2016, 18:49

Re: Classes in AHK, Basic tutorial.

14 Nov 2016, 07:10

good tutorial and helpful comments ty
stealzy
Posts: 91
Joined: 01 Nov 2015, 13:43

Re: Classes in AHK, Basic tutorial.

15 Feb 2017, 09:48

Code: Select all

Class A {
	Static x := 1
	y := 2
	__New() {
		This.z := 3 ; there should be a function, there is the Static section for constants
	}
}
B := A, C := new A
MsgBox % "x=" B.x "`ty=" B.y "`tz=" B.z "`nx=" C.x "`ty=" C.y "`tz=" C.z
pk23
Posts: 110
Joined: 24 Apr 2015, 00:49

Re: Classes in AHK, Basic tutorial.

06 Apr 2017, 04:59

This is what I'm looking for!
Help file is so hard to read. Many thanks @samardac !
Painterboy
Posts: 5
Joined: 29 Apr 2014, 23:06

Re: Classes in AHK, Basic tutorial.

09 Jan 2018, 16:37

Many thanks, I have been using AHK for a while and only now starting to dive into classes. Your tutorial helped me a lot.

Thanks Samardac :-)
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: Classes in AHK, Basic tutorial.

09 Jan 2018, 17:23

How chancy. I was reading this today too
try it and see
...
Painterboy
Posts: 5
Joined: 29 Apr 2014, 23:06

Re: Classes in AHK, Basic tutorial.

09 Jan 2018, 19:07

derz00 wrote:How chancy. I was reading this today too
Haha, just goes to show how important all these tutorials are. I Iooked into classes and objects before but they were a bit more then I could handle at the time. This time around I feel like I am starting to have a better grasp. I still a long way to go but this tutorial really helped me a lot.
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: Classes in AHK, Basic tutorial.

03 Jun 2018, 07:21

Hello and thanks For this tutorial! I still have one question which is major: why would we use classes instead of named variables like : sales_window, sales_ItemName
Or why wouldnt we just use clearly labelled functions like : sales_GetItemName()

I Just dont get why complicate understanding of script with class?
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Classes in AHK, Basic tutorial.

03 Jun 2018, 08:59

https://autohotkey.com/boards/viewtopic.php?f=7&t=41332
Classes only become useful when using them correctly.
Recommends AHK Studio
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: Classes in AHK, Basic tutorial.

03 Jun 2018, 20:48

nnnik wrote:https://autohotkey.com/boards/viewtopic.php?f=7&t=41332
Classes only become useful when using them correctly.
Wow thats a huge answer haha :D thanks alot gonna read through your detailed topic :)

Return to “Tutorials (v1)”

Who is online

Users browsing this forum: No registered users and 25 guests