Jump to content


How to use a "if_then_, if not_then_Conditionals PLEASE HELP


  • Please log in to reply
7 replies to this topic

#1 yojake

yojake
  • Guests

Posted 06 May 2012 - 01:10 AM

Hey guys i'm wondering how to do conditionals on ahk. More specifically, I would like the mouse to go to a certain spot on the screen and click if the color of the spot the mouse is at is a certain color. Like when you go to window spy, it tells you the mouse position and the color. I want it to click if color is .... doesn't really matter... and click somewhere else if not. How do you do this?? Thanks!

#2 Leef_me

Leef_me
  • Moderators
  • 7704 posts

Posted 06 May 2012 - 01:36 AM

Hi yojake, welcome to the forum.

Have you worked through the tutorials?
<!-- m -->http://www.autohotke...cs/Tutorial.htm<!-- m -->
<!-- l --><a class="postlink-local" href="http://www.autohotkey.com/community/viewtopic.php?f=1&t=47791">viewtopic.php?f=1&t=47791</a><!-- l -->

Have you tried to write the script ? Have you searched the forum ?

Here is the list of commands <!-- m -->http://www.autohotke...cs/commands.htm<!-- m -->
The list is also availbe in the AHK helpfile.

The following commands seem best for your task.
Please read the docs for them. Each has a short example of how to used the command.
PixelGetColor
MouseClick
MouseMove
if
if (expression)
MouseGetPos

#3 Nailbiter

Nailbiter
  • Guests

Posted 06 May 2012 - 01:38 AM

You said:

I would like the mouse to go to a certain spot on the screen and click if the color of the spot the mouse is at is a certain color.

#SingleInstance, Force
coordmode, pixel, screen
coormode, mouse, screen

targetcolor = 0xFFFFFF [color=#008000];<-- this is a white pixel put your own target color here[/color]
mouseX = 0 [color=#008000];<-- put the mouse x position here[/color]
mouseY = 0 [color=#008000];<-- put the mouse y position here[/color]
return

!k:: [color=#008000];<--hotkey to do the scan[/color]
pixelgetcolor, thiscolor, %mouseX%, %mouseY%, RGB
if (thiscolor = targetcolor)
{      MouseMove, %mouseX%, %mouseY% 
      click
}
Return

And here is a little routine that will help you find the right colors and locations to click...
use Alt-g to start the scan and the middle mouse button to grab the color and location.
It puts the color and location on the clipboard as a CSV item that can be pasted into any text file.
Use shift-Esc to stop the scanner. Ctrl-V can paste the data into your script.
;================================================
; A simple grabber routine for finding locations
; and colors on game screens.
;
; Use Alt-g to start grab or to abort a grab in progress
; use Middle Mouse Button to grab the location and color
; where the mouse currently points. The X and Y location
; along with the the Color in RGB format at that location
; is saved to the clipboard for pasting with Ctrl-V into
; a script or other document.
; This routine uses a 100 ms timer to keep track of the 
; cursor as it move around on the screen
;------------------------------------------------
!g::
GrabFlag := (!GrabFlag) ;<-- toggle grab flag on and off
If (!GrabFlag) ;<-- turn off
{	SetTimer, WatchMouse, Off ;<-- turn off timer
	Sleep, 100
	tooltip		;<-- turn off x/y/color tooltip if on
	Return
}
; grabbing so get old mouse pos to save for later
MouseGetPos, oposx, oposy ;<-- save old mouse position
Sleep 100
SetTimer, WatchMouse, 100 ;<-- 100 ms timer for x/y/color tooltip
Return

;------------------------------------------------
; 100 ms timer watches the mouse position
;------------------------------------------------
WatchMouse:
If (GrabFlag = 0)
{	Tooltip
	Return
}
MouseGetPos, mx, my
Sleep 100
PixelGetColor, mc, %mx%, %my%, RGB
Sleep 100
ToolTip, Activate window - MButton to grab x=%mx% y=%my% c=%mc%
return

;------------------------------------------------
; used to signal a location grab
;------------------------------------------------
$MButton::
Tooltip
If (GrabFlag) ; If grabbing loc and color
{	GrabFlag = 0
	
	; turn off the mouse watch timer
	SetTimer, WatchMouse, Off
	Sleep 1000
	
	; Save the data on the clipboard for use with ctrl-v or "paste"
	MouseMove, %oposx%, %oposy%
	Sleep 500
	PixelGetColor, c1, %mx%, %my%, RGB
	Sleep, 300
	Tooltip
	
	; put the grab on the clipboard for pasting into script or file
	Clipboard = %mx%,%my%,%mc%
	;str = %mx%,%my%,%mc%
	ClipWait
	Tooltip
	Return
}

; otherwise just send the mbutton press on to the app
Send, {MButton}
Sleep, 200
Return

;------------------------------------------------
; a handy way to exit the script
;------------------------------------------------
+Esc::ExitApp


#4 yojake

yojake
  • Guests

Posted 06 May 2012 - 04:18 PM

Thanks! I'm looking for the first one. I have the mouse color and location using windows spy. Problem is, how can i make a script that will click somewhere else if the color isnt the targetcolor?

Btw im new to this :p so sorry if i seem dumb...

#5 yojake

yojake
  • Guests

Posted 06 May 2012 - 04:26 PM

Oh hey. That script you gave me says there is an error with the line "coordmode, mouse, screen"

#6 yojake

yojake
  • Guests

Posted 06 May 2012 - 04:26 PM

oops. the line "coormode, mouse, screen"

#7 Nailbiter

Nailbiter
  • Guests

Posted 06 May 2012 - 09:47 PM

nice catch. change coormode to coordmode

#8 OldBloke

OldBloke
  • Members
  • 42 posts

Posted 07 May 2012 - 01:09 AM

Thanks! I'm looking for the first one. I have the mouse color and location using windows spy. Problem is, how can i make a script that will click somewhere else if the color isnt the targetcolor?

Add an Else routine after the IF routine at the end of nailbiter's script, before the Return.

Else
{ MouseMove, %mouseX2%, %mouseY2%
click
}
Return

You can define the second set of variables near the top of the script, where nailbiter wrote "mouseX = 0", or just write the actual numbers there instead of using variables.

MouseMove, 75, 100