Help with a simple IF statement

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
RozRoyal
Posts: 24
Joined: 21 Nov 2017, 14:19

Help with a simple IF statement

21 Nov 2017, 16:43

How to make an IF statement

when number 1 is pressed AND the program is running (test.xspf) move the mouse to x location

if (test.xspf) is not then open when number 1 is pressed than run the file (test.xspf)

If someone could help me with this I would be truly grateful!

Here is where my test.xspf is if that helps
1::Run, C:\Users\Andrew\Desktop\test.xspf
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Help with a simple IF statement

21 Nov 2017, 17:15

Hi RozRoyal and welcolme to the ahk community.
Here's a simple commented example with notepad.

Code: Select all

#IfWinExist ahk_exe notepad.exe ; create a context-sensitive hotkey depending on the wintitle parameter specified (here any window which belongs to notepad process - modify it to make it fit your purpose)
1::MouseMove, 0, 0 ; 
#IfWinNotExist ahk_exe notepad.exe ; change the context-sensitivity for subsequently created hotkeys
1::
run, notepad
MsgBox, test
return ; if a hotkey needs to execute more than on line, those lines cannot be listed to the right of the double-colon nor should be placed beneath it and  a return used to finish the subroutine
#If ; turn off context sensitivity

references:
#If (context-sensitive hotkey)
win title parameter
my scripts
Baba Katchka

Re: Help with a simple IF statement

22 Nov 2017, 08:51

A_AhkUser wrote:Hi RozRoyal and welcolme to the ahk community.
Here's a simple commented example with notepad.

Code: Select all

#IfWinExist ahk_exe notepad.exe ; create a context-sensitive hotkey depending on the wintitle parameter specified (here any window which belongs to notepad process - modify it to make it fit your purpose)
1::MouseMove, 0, 0 ; 
#IfWinNotExist ahk_exe notepad.exe ; change the context-sensitivity for subsequently created hotkeys
1::
run, notepad
MsgBox, test
return ; if a hotkey needs to execute more than on line, those lines cannot be listed to the right of the double-colon nor should be placed beneath it and  a return used to finish the subroutine
#If ; turn off context sensitivity

references:
#If (context-sensitive hotkey)
win title parameter
just curious why you used #ifwinexist over ifwinexist, sorry just trying to learn the difference, the documentation has be confused
User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: Help with a simple IF statement

22 Nov 2017, 09:12

Baba Katchka wrote: just curious why you used #ifwinexist over ifwinexist, sorry just trying to learn the difference, the documentation has be confused
By using #ifwinexist he is saying that the hotkey will only work if the condition is met.

with it set up that way, pressing 1 in any other window will do what it normally would do, and the hotkey would only become active in the targeted window.
RozRoyal
Posts: 24
Joined: 21 Nov 2017, 14:19

Re: Help with a simple IF statement

22 Nov 2017, 10:41

I have a small issue, I want to double-click on a location 1225, 128 (that would be on the playlist) but for some reason, it clicks down-left of the screen...

Why is this the case?

Code: Select all

#NoEnv 
SendMode Input 
SetWorkingDir %A_ScriptDir%  

#IfWinExist Playlist ahk_class QWidget
1::Click 2, 1225, 128
#IfWinNotExist ahk_exe vlc.exe
1::run, C:\Users\Andrew\Desktop\test.xspf
return
#If
Image
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Help with a simple IF statement

22 Nov 2017, 12:51

RozRoyal wrote:I have a small issue, I want to double-click on a location 1225, 128 (that would be on the playlist) but for some reason, it clicks down-left of the screen...
Why is this the case?
I guess it is due to the fact that:
Coordinates are relative to the active window unless CoordMode was used to change that.
source:MouseMove


So, using CoordMode you'll be able to set coordinates to be relative to the screen (top-left part of your screen as origin (0, 0)):

Code: Select all

CoordMode, Mouse, Screen
You also can choose to substitute both absolute coordinates 1225 and 128 by the relatives one; so that your script will be more reliable since it will work no matter where the window is located.
my scripts
RozRoyal
Posts: 24
Joined: 21 Nov 2017, 14:19

Re: Help with a simple IF statement

22 Nov 2017, 14:13

I'm so almost done with my first script! Thank you AhkUser for your help!

Just one more thing I think haha

When I press left key I want it to ONLY click if the mouse cursor is in one of the 3 locations (280, 1045 or 680, 1045 or 780, 1045)

Code: Select all

left::click if mouselocation 280, 1045 or 680, 1045 or 780, 1045
The code above does not work : (
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Help with a simple IF statement

22 Nov 2017, 17:07

I suggest that you make use of the help file (AutoHotkey.chm) that comes with AutoHotkey to lean the syntax of commands and which commands you can use, so that you aren't just stumbling in the dark and guessing at them. For now, though, the following seems to be what you want:

Code: Select all

left::
  MouseGetPos, MouseX, MouseY
  If ((MouseX = 280) and (MouseY = 1045)) or ((MouseX = 680) and (MouseY = 1045)) or ((MouseX = 780) and (MouseY = 1045))
    Click %MouseX%, %MouseY%
return
I suggest looking up the MouseGetPos, If and Click commands in the help file (you can also click on them above) to familiarize yourself with them so that you understand what the above code does and how you might modify it to do something different.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Help with a simple IF statement

22 Nov 2017, 20:38

You also can use IfIn when many values can meet a condition:

Code: Select all

Left::
MouseGetPos, mouseX, mouseY
if (mouseY = 1045)
	if mouseX in 280,680,780
		MsgBox, test
return
my scripts
RozRoyal
Posts: 24
Joined: 21 Nov 2017, 14:19

Re: Help with a simple IF statement

22 Nov 2017, 21:29

Thank you! A_AhkUser and Osprey! : )
RozRoyal
Posts: 24
Joined: 21 Nov 2017, 14:19

Re: Help with a simple IF statement

26 Nov 2017, 22:11

Code: Select all

Left::
MouseGetPos, mouseX, mouseY
if (mouseY = 1045)
	if mouseX in 280,680,780
Click %MouseX%, %MouseY%
return  
How can I make the number be in range? For example, look from 80 to 1870?

I tried

Code: Select all

if mouseX in (1870, 80)

Code: Select all

if mouseX in (1870-80)
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Help with a simple IF statement

26 Nov 2017, 22:26

Some possibilities:

Code: Select all

;if vNum between %vLim1% and %vLim2%
Between(vNum, vLim1, vLim2)
{
	return (vNum >= vLim1 && vNum <= vLim2)
}

if (mouseX >= 1870 && mouseX <= 1880)
if mouseX between 1870 and 1880 ;due to be removed in AHK v2
if Between(mouseX, 1870, 1880) ;requires a custom function
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
RozRoyal
Posts: 24
Joined: 21 Nov 2017, 14:19

Re: Help with a simple IF statement

26 Nov 2017, 22:42

Thank you Jeeswg!
RozRoyal
Posts: 24
Joined: 21 Nov 2017, 14:19

Re: Help with a simple IF statement

30 Nov 2017, 23:41

Hey all! Hopefully, someone can help me with this small question.

How can I add an exemption for the below code

Code: Select all

#IfWinExist rozgrzewka
left::p ;skip to previous song
right::n ;skip to next song
return
to make it work with the main code.

Code: Select all

Between(vNum, vLim1, vLim2)
{
	return (vNum >= vLim1 && vNum <= vLim2)
} 

Left::
MoeGetPos, mouseX, mouseY
#if (mouseY = 1045)
	#if (mouseX >= 80 && mouseX <= 1860)
Click %MouseX%, %MouseY%
return

right::toggle:=!toggle
#if toggle
up::Send {+}
down::Send {-}
#if !toggle
up::Send {Volume_Up}
down::Send {Volume_Down}
#if

#IfWinExist Polka_nr_13.mp3
1::click 80, 1045
	2::click 880, 1045
	3::click 980, 1045
return
What I want is to skip the songs using buttons (left or right) only if the window (Rozgrzewka) exists.

If it doesn't exist then the main code works.

Thank you.
Last edited by RozRoyal on 01 Dec 2017, 12:36, edited 1 time in total.
RozRoyal
Posts: 24
Joined: 21 Nov 2017, 14:19

Re: Help with a simple IF statement

01 Dec 2017, 12:28

bump, this is the only things missing in my remote script : (
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Help with a simple IF statement

01 Dec 2017, 17:31

If you're saying that combining the two sets of code makes the second set not execute, it may be because you have "return" in the first set. Remove that.
Last edited by Osprey on 01 Dec 2017, 20:26, edited 1 time in total.
RozRoyal
Posts: 24
Joined: 21 Nov 2017, 14:19

Re: Help with a simple IF statement

01 Dec 2017, 19:21

That didn't work : (

Maybe my adding

Code: Select all

#IfWinNotExist Rozgrzewka
to Left and Right, might work?

Code: Select all

Left::
#IfWinNotExist Rozgrzewka ; < ------------------- HERE
MoeGetPos, mouseX, mouseY
#if (mouseY = 1045)
	#if (mouseX >= 80 && mouseX <= 1860)
Click %MouseX%, %MouseY%
return

right::toggle:=!toggle
#IfWinNotExist Rozgrzewka ; < ------------------- HERE
#if toggle
up::Send {+}
down::Send {-}
#if !toggle
up::Send {Volume_Up}
down::Send {Volume_Down}
#if
But that didn't work.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 124 guests