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] [Download] (Script.ahk)GeSHi © Codebox Plus

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] [Download] (Script.ahk)GeSHi © Codebox Plus

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] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus


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] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus


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

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus



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] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus



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] [Download] (Script.ahk)GeSHi © Codebox Plus

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] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus



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] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus



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] [Download] (Script.ahk)GeSHi © Codebox Plus

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] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus



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] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus



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: 180
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] [Download] (Script.ahk)GeSHi © Codebox Plus

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: 335
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: 33
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: 42
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: 2278
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: 3564
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] [Expand] [Download] GeSHi © Codebox Plus


For the scope of this tutorial, I think code like the above would only confuse them.
User avatar
megnatar
Posts: 63
Joined: 27 Oct 2014, 20:49
GitHub: Megnatar
Location: The Netherlands

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: 51
Joined: 01 Nov 2015, 13:43

Re: Classes in AHK, Basic tutorial.

15 Feb 2017, 09:48

Code: [Select all] [Download] GeSHi © Codebox Plus

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: 47
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 !

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 7 guests