Calling from another script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
r3dp1ll

Calling from another script

16 Aug 2018, 06:17

Hello everybody,

I am trying to call and use a function from another file (script .ahk). It seems to be calling and working BUT everything in the IF, it does not work. What's wrong with my code ?

File "main" :

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
;SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#include %A_ScriptDir%/L2P/L2P_clean.ahk 

Loop = "1"

while (Loop = "1")
{
q::
ToRight(Mid,moka,x,a,e)
return

d::
ToLeft(Mid,moka,x,a,e)
return
}
return

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
;SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

; To the right
ToRight(currentPosture,opName,KeyBlock,KeyLLeft,KeyLRight)
{
if (%currentPosture% != "Droi" ) && (%opName% != "kimo") && (%opName% != "kraga")
{
	GetKeyState, stateKeyBlock, %KeyBlock%
    if (stateKeyBlock = "U")
	{
		send, {%KeyLLeft% up}
		send, {%KeyLRight% down}
		currentPosture := "Droi"
	}
	return
}
sleep, 10
return
}


; to the left
ToLeft(currentPosture,opName,KeyBlock,KeyLLeft,KeyLRight)
{
if (currentPosture != "Gau" ) && (opName != "kimo") && (opName != "kraga")
{
	GetKeyState, stateKeyBlock, %KeyBlock%
    if (stateKeyBlock = "U")
	{	
		send, {%KeyLRight% up}
		send, {%KeyLLeft% down}
		currentPosture := "Gau"
	}
	return
}
sleep, 10
return
}
I thought it came from the parameters if the "if", so I tried with or without %, it dont work.

Thank you for your help.
YMP2
Posts: 48
Joined: 20 Apr 2014, 06:55

Re: Calling from another script

16 Aug 2018, 13:34

Code: Select all

ToLeft(Mid,moka,x,a,e)
If the things in parentheses are literal strings and symbols, you should put them in quotes.

Code: Select all

if (%currentPosture% != "Droi" ) && (%opName% != "kimo") && (%opName% != "kraga")
Remove the percent signs.
r3dp1ll

Re: Calling from another script

22 Aug 2018, 07:08

hi,

Than you for your help @YMP2, sorry for the response time, I was very busy.

Indeed, the code is cleaner like that. But it still does not work.

Well I made some changes to understand where the problem is. With this:

main.ahk :

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
;SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#include %A_ScriptDir%/L2P/L2P_clean.ahk 

Loop = "1"

while (Loop = "1")
{
q::
ToRight("Mid","moka",x,a,e)
return

d::
ToLeft("Mid","moka",x,a,e)
return

f::
ToLeft2("Mid","moka",x,a,e)
return

h::
ToRight2("Mid","moka",x,a,e)
return

}
return
L2P_clean.ahk :

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
;SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

; To the right
ToRight(currentPosture,opName,KeyBlock,KeyLLeft,KeyLRight)
{
if (currentPosture != "Droi" ) && (opName != "kimo") && (opName != "kraga")
{
	GetKeyState, stateKeyBlock, %KeyBlock%
    if (stateKeyBlock = "U")
	{
		send, {%KeyLLeft% up}
		send, {%KeyLRight% down}
		currentPosture := "Droi"
	}
	return
}
sleep, 10
return
}


; to the left
ToLeft(currentPosture,opName,KeyBlock,KeyLLeft,KeyLRight)
{
if (currentPosture != "Gau" ) && (opName != "kimo") && (opName != "kraga")
{
	GetKeyState, stateKeyBlock, %KeyBlock%
    if (stateKeyBlock = "U")
	{	
		send, {%KeyLRight% up}
		send, {%KeyLLeft% down}
		currentPosture := "Gau"
	}
	return
}
sleep, 10
return
}

; To the right2
ToLeft2(currentPosture,opName,KeyBlock,KeyLLeft,KeyLRight)
{
if (currentPosture != "Droi" ) && (opName != "kimo") && (opName != "kraga")
{
		send "okok"
		send, {%KeyLLeft% up}
		send, {%KeyLRight% down}
		currentPosture := "Droi"
}
sleep, 10
return
}

; To the right2
ToRight2(currentPosture,opName,KeyBlock,KeyLLeft,KeyLRight)
{
if (currentPosture != "Droi" ) && (opName != "kimo") && (opName != "kraga")
{
	GetKeyState, stateKeyBlock, %KeyBlock%
    if (stateKeyBlock = "U")
	{
		send "okok"
		send, {%KeyLLeft% up}
		send, {%KeyLRight% down}
		currentPosture := "Droi"
	}
	return
}
sleep, 10
return
}
The only key that makes "something" (and not correctly) is the h, who writes to me okok.

I deduce that it is:
- either the transfer of keys from one script to another. (ex : x for KeyBlock)
- Either the transfert is good, but the use is bad or different from usual, and i dont know that.

What do you think ? an idea ?
YMP2
Posts: 48
Joined: 20 Apr 2014, 06:55

Re: Calling from another script

23 Aug 2018, 00:48

Why are the last three parameters (x,a,e) not in quotes? As far as I can see they are the names of keys, not variable names.

Also, putting hotkey definitions in a While block makes no sense. Remove it.
r3dp1ll

Re: Calling from another script

23 Aug 2018, 03:49

Hi, thank you for your help and your responsiveness.

I did not put them in quote because for me, only variables with a type literal string are in quote. I thought it was variable names (more precisely function names). That in the software ahk, there were functions already created (the hotkeys) which allowed to verify the keys of the keyboard when they are pressed and to make an action that the user chose... the famous

Code: Select all

key::
Ok, I will try to explain clearly my logic (which apartment is NOT logical). Maybe you can explain to me what I do not understand.
what makes me confused is this:
When I use the fonction "send" and want to maintain a key pressed, I do not have the impression to be able to use a literal string. Example:

Code: Select all

send {s down}
works while

Code: Select all

send {"w" down}
dont work.
For me I explained this on the fact that the S here is not treated as a literal strings, but as the name of an embedded function in ahk.

Finally, I decided not to put x, a, e in quotes, because I'm afraid it tries to make me a :

Code: Select all

send {"w" down}
instead of a :

Code: Select all

send {s down}
, and that it does not work.

So for me, in a

Code: Select all

send {key down}
I'm calling a function key names and i'm not declaring a new varible of type of literal string, like i can do with :
send "x"
For the "while", I do not remember why I did that ... :crazy: Maybe it was to prevent the script from closing too fast. But I think that hotkeys functions are infinite loops so it would not be closed, it was stupid lol

Anyway everything works, thank you so much for your help. That's really nice.

PS :
1) at the same time that I write I have just tried a
send "{w down}"
. It sends me a lot of "w". I deduce that "send" is a function. That everything we write behind will be a parameter of this function and will be taken for a literal string. BUT I have the impression that the function "send" ensures that in its parameters there is no "{key down}", and that if there is any, it does not treat it like literal and understand that it is necessary to maintain this button and do not display {key down}.
2) I need to put quotes on x,a,e because my functions KeyBlock,KeyLLeft,KeyLRight need to know that they are literal, because it is only later that this literal string will be converted into function
3) I understand that my compiler dont read a

Code: Select all

send {"x" down}
, because my function KeyBlock sends him simply a x as I could write it on the keyboard, my function KeyBlock does not send it parentheses as "X". That is the send function who take the literal x and use it for a parameter of another function.
i'm right ? :s

Sorry if in my head it's confused, I'm still learning the development
YMP2
Posts: 48
Joined: 20 Apr 2014, 06:55

Re: Calling from another script

23 Aug 2018, 07:10

Yes, there is ambiguity in AutoHotkey because it has functions and commands. Functions are those you call with (). They take literals in "" and variables without %%. Commands take literals without "" and require %% if you pass the content of a variable rather than its name (in the latter case you use neither "" nor %%). Some parameters of some commands expect the name of a variable — for example, if it's an output parameter. So, there is no other way but read the description of a command to figure out how it works.

Send is a command and ToRight is a function, hence the difference in how you pass arguments to them.
r3dp1ll

Re: Calling from another script

25 Aug 2018, 08:42

@YMP2 oh, ok. It's good to know !

Thank you very much for giving me this tips and for your help !

Have a nice day ;)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, jaka1, mikeyww, Rohwedder and 309 guests