Trying to do a switch, didn't work

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Madcore
Posts: 3
Joined: 11 Mar 2018, 09:24

Trying to do a switch, didn't work

11 Mar 2018, 09:50

Hi. Im trying to do a switch, because I'm using intercept to launch scripts from a second keyboard, but some things doesn't work right.

I'm trying to send a space down command and write an option on a ini file and use it to detect is the space down is still active. My code is like this:

Code: Select all

numpad0::
state1 := "%A_ScriptDir%\states.ini"
IniRead, nstate, %state1%, Teclas, cero
if %nstate% = 0
{
	IniWrite, 1, %state1%, Teclas, cero

	SendInput, {space down}
	
	return
}
else
{
	IniWrite, 0, %state1%, Teclas, cero

	SendInput, {space up}

	return
}
I don't know why it didn't work. It didn't read or write anything from the ini file, and don't do anything. I think I wrote the code right, but It's obvious it itsn't.

Anyone know how to fix that and make it work? I spend some days trying to figure out why is failing.

Thanks.
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: Trying to do a switch, didn't work

11 Mar 2018, 10:19

try:

Code: Select all

numpad0::
state1 := A_ScriptDir "\states.ini"
IniRead, nstate, %state1%, Teclas, cero
if %nstate% = 0
{
	IniWrite, 1, %state1%, Teclas, cero

	SendInput, {space down}
	
	return
}
else
{
	IniWrite, 0, %state1%, Teclas, cero

	SendInput, {space up}

	return
}
gregster
Posts: 9114
Joined: 30 Sep 2013, 06:48

Re: Trying to do a switch, didn't work

11 Mar 2018, 11:02

Your if-syntax is faulty. There is traditional and expression if-syntax - in both cases the variable name mustn't have %s.
Use either if nstate = 0 ()traditional) or if (nstate = 0) (expression). There is a bigger difference between these two, if the value is a variable reference instead.
Expression syntax is recommended: https://autohotkey.com/docs/commands/IfExpression.htm
Madcore
Posts: 3
Joined: 11 Mar 2018, 09:24

Re: Trying to do a switch, didn't work

11 Mar 2018, 11:54

Thanks Johana! It works flawless.
gregster
Posts: 9114
Joined: 30 Sep 2013, 06:48

Re: Trying to do a switch, didn't work

11 Mar 2018, 12:02

Still your if-statement is most probably not doing what you think it is doing. What is the content of nstate that you read from the ini-file?

Compare these to ifs

Code: Select all

nstate := 3

if nstate = 3			; true 3 = 3
	msgbox 1

if %nstate% = 3			; false 0 = 3
	msgbox 2
In your case, you are probably just lucky because nstate is 0 or empty - if it is not, you are out of luck.

Code: Select all

nstate := 0

if nstate = 0			; true 0 = 0
	msgbox 1

if %nstate% = 0			; true 0 = 0 - but only by chance
	msgbox 2
%nstate% is here looking for a variable named 0 which doesn't exist and therefore interpreted as empty/zero. Therefore, the first example doesn't work like you expect. The variable named 3 is also empty/zero and not 3.
This is caused by AHK's rather lenient variable handling.
Madcore
Posts: 3
Joined: 11 Mar 2018, 09:24

Re: Trying to do a switch, didn't work

11 Mar 2018, 14:53

gregster wrote:Your if-syntax is faulty. There is traditional and expression if-syntax - in both cases the variable name mustn't have %s.
Use either if nstate = 0 ()traditional) or if (nstate = 0) (expression). There is a bigger difference between these two, if the value is a variable reference instead.
Expression syntax is recommended: https://autohotkey.com/docs/commands/IfExpression.htm
Thanks for this. I corrected my code and work like a charm. Here it is:

Code: Select all

	state1 := A_ScriptDir "\states.ini"
	IniRead, photoshopspace, %state1%, Photoshop, espacio
	if photoshopspace = 0
	{
		IniWrite, 1, %state1%, Photoshop, espacio

		SendInput, {space down}
		
		return
	}
	else
	{
		IniWrite, 0, %state1%, Photoshop, espacio

		SendInput, {space up}

		return
	}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww, Spawnova and 339 guests