Merge functions? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Merge functions?

25 May 2018, 10:25

Hi,

In this simple example script I would like to do the following:

Key 1::
- Function 1
- specific commands for key 1
- Function 2

Key 2::
- Function 1
- specific commands for key 2
- Function 2

Key 3::
- Function 1
- specific commands for key 3
- Function 2

Function 1 and Function 2 contain commands which are valid for all keys.
The specific commands are different from key to key.

It could be something like:
Function 1: Write introduction
key x: write specific text
Function 2: write concluding part

Of course this works, but I'm wondering if I can merge Function 1 and 2 but define that a key must be pressed
to continue with the function.

The example code:

Code: Select all

1::
Function1()
Send first
Function2()
return

2::
Function1()
Send second
Function2()
return

3::
Function1()
Send third
Function2()
return

Function1()
{
  Send This is the
  Sleep, 5
  Send {Space}
}

Function2()
{
  Send {Space}
  Sleep, 5
  Send test.
}
I wonder if I can merge both functions...

Regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Merge functions?

25 May 2018, 12:31

Scr1pter wrote:...I'm wondering if I can merge Function 1 and 2 but define that a key must be pressed
to continue with the function.
Just so I understand, would the continuation be contingent upon whether a specific hotkey is pressed?
For instance:

Code: Select all

1::function1(), function2()
2::function1(), function2()

function1()
{
	msgbox % A_ThisFunc " called by pressing " A_ThisHotkey
}


function2()
{
	if ( A_ThisHotkey = 1 )
	{
		msgbox % A_ThisFunc " called by pressing " A_ThisHotkey	
	}
	else 
	{
		msgbox % A_ThisFunc " called but please press 4 after this messagebox to continue"
		KeyWait, 4, D
		msgbox You have continued by pressing 4
	}
}
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Merge functions?  Topic is solved

26 May 2018, 05:40

Hello,

Well, I solved my issue a bit differently.
However, your post helped me get the idea.

Instead of executing an operation when pressing the hotkey,
I only assign a variable.

Within the function (I merged function 1 and 2), I created an if statement
which checks the value of the variable.
Depending on the value, another "Send..." gets executed.

On my target script (which is of course different), my logic works as well.


Thanks for your support!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
asad41163
Posts: 268
Joined: 29 Jul 2014, 14:31

Re: Merge functions?

26 May 2018, 08:01

Hi everyone
Scr1pter wrote: ,,"Well, I solved my issue a bit differently.
However, your post helped me get the idea."
good,
Please, put the code here to learn and take advantage of the idea.
thanks
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Merge functions?

26 May 2018, 08:56

im also curious, as it eludes me why youd break the nice template method you originally had in there in lieu of something, most likely, less robust
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: Merge functions?

26 May 2018, 09:09

I second @swagfag, you should use @TLM's method,it's not only leaner & robust, but also faster.
Outsourcing Clicks & Presses Since 2004.
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Merge functions?

27 May 2018, 10:27

Hi,

Well, as said, my target script is a bit different, so I don't know if the suggested method will work properly.

Here is my method for my target script:

Code: Select all

+F8:: ; Shift+F8
picture = testpic1.png
SearchPic()
return

+F9:: ; Shift+F9
picture = testpic2.png
SearchPic()
return

+F10:: ; Shift+F10
picture = testpic.png
SearchPic()
return

SearchPic()
{
  Send ^{F4} ; Force browser window on
  Sleep, 5  
  ImageSearch, x, y, 0, 0, 530, 520, %A_ScriptDir%\pics\%picture% ; Search for the chosen picture
  if ErrorLevel = 0
  {
    MouseMove, x+10, y+5 ; Move cursor to the destination line
    Sleep, 5
    Send {lbutton} ; Click on the picture
  }
  if ErrorLevel = 1
  {
    MsgBox, Picture was not found!
  }
}
Originally, I would have split the SearchPic() function into two function as I did it in post 1.
But since I use variables (assigned to keys), there is no need to split the function anymore.

Regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Merge functions?

27 May 2018, 12:54

as a head up, this snippet wont work as expected without modifications:

Code: Select all

 ImageSearch, x, y, 0, 0, 530, 520, %A_ScriptDir%\pics\%picture%
the %picture% in here doesnt refer to the picture youve assigned outside the function. see function scoping in the docs
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Merge functions?

27 May 2018, 14:34

Yes I know.
That's why I have
global picture
in the header of the script.

But thanks anyway :)
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: Merge functions?

29 May 2018, 07:37

This is what i meant.

Code: Select all

+F8::SearchPic("testpic1.png") ; Shift+F8
+F9::SearchPic("testpic2.png") ; Shift+F9
+F10::SearchPic("testpic.png") ; Shift+F10


SearchPic(picture)
{
  Send ^{F4} ; Force browser window on
  Sleep, 5  
  ImageSearch, x, y, 0, 0, 530, 520, %A_ScriptDir%\pics\%picture% ; Search for the chosen picture
  if !ErrorLevel{		;If ErrorLevel is zero or 'false'
    MouseMove, x+10, y+5 ; Move cursor to the destination line
    Sleep, 5
    Send {lbutton} ; Click on the picture
  }Else If ErrorLevel{		;If ErrorLevel is nonZero or 'true'
    MsgBox, Picture was not found!
  }	
}


Outsourcing Clicks & Presses Since 2004.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], MrDoge, Rohwedder and 247 guests