Press Keystroke if Color on Screen is detected Topic is solved

Ask gaming related questions
Mystarix
Posts: 5
Joined: 23 Mar 2023, 06:39

Press Keystroke if Color on Screen is detected

Post by Mystarix » 23 Mar 2023, 06:44

Hello. I did find some v1 Examples but they didn't work for me, so i thought i'd ask for a v2 Code.

I need a script that loops and everytime the color 0x43948D is visible in the game on Monitor 1 press the keystroke E.
Is there a way to do this?

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Press Keystroke if Color on Screen is detected

Post by mikeyww » 23 Mar 2023, 10:04

Welcome to this AutoHotkey forum!

Code: Select all

#Requires AutoHotkey v2.0
game := 'ahk_exe notepad.exe'
rgb  := 0x43948D
WinGetClientPos &x1, &y1, &width, &height, game
CoordMode 'Pixel'
WinActivate game
SetKeyDelay , PRESSDURATION := 25
Loop {
 Sleep 30
 If PixelSearch(&x, &y, x1, y1, x1 + width - 1, y1 + height - 1, rgb)
  SendEvent 'e'
}

Mystarix
Posts: 5
Joined: 23 Mar 2023, 06:39

Re: Press Keystroke if Color on Screen is detected

Post by Mystarix » 25 Aug 2023, 21:06

mikeyww wrote:
23 Mar 2023, 10:04
Welcome to this AutoHotkey forum!
Hey, thank you very much for the Hotkey. Is there any way to extend the code so that it counts the amount of times it detected the color and presses an additional hotkey (e) certain milliseconds after for example the fifth color detection?
So:

Detected Color -> E
Detected Color -> E
Detected Color -> E
Detected Color -> E
Detected Color -> E
Detected the color 5 times -> wait some ms -> press e, reset count

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Press Keystroke if Color on Screen is detected  Topic is solved

Post by mikeyww » 26 Aug 2023, 01:20

Code: Select all

#Requires AutoHotkey v2.0
game := 'ahk_exe notepad.exe'
rgb  := 0x43948D
WinGetClientPos &x1, &y1, &width, &height, game
CoordMode 'Pixel'
WinActivate game
SetKeyDelay , PRESSDURATION := 25
n  := 0
x2 := x1 + width  - 1
y2 := y1 + height - 1
Loop {
 Sleep 30
 If PixelSearch(&x, &y, x1, y1, x2, y2, rgb) {
  SendEvent 'e'
  If ++n = 5 {
   Sleep 100
   SendEvent 'e'
   n := 0
  }
 }
}

Mystarix
Posts: 5
Joined: 23 Mar 2023, 06:39

Re: Press Keystroke if Color on Screen is detected

Post by Mystarix » 26 Aug 2023, 08:08

Hey @mikeyww
this works perfect, thank you.

Mystarix
Posts: 5
Joined: 23 Mar 2023, 06:39

Re: Press Keystroke if Color on Screen is detected

Post by Mystarix » 04 Oct 2023, 10:19

mikeyww wrote:
26 Aug 2023, 01:20

Code: Select all

#Requires AutoHotkey v2.0
game := 'ahk_exe notepad.exe'
rgb  := 0x43948D
WinGetClientPos &x1, &y1, &width, &height, game
CoordMode 'Pixel'
WinActivate game
SetKeyDelay , PRESSDURATION := 25
n  := 0
x2 := x1 + width  - 1
y2 := y1 + height - 1
Loop {
 Sleep 30
 If PixelSearch(&x, &y, x1, y1, x2, y2, rgb) {
  SendEvent 'e'
  If ++n = 5 {
   Sleep 100
   SendEvent 'e'
   n := 0
  }
 }
}
Is there any way to extend this a little so that it runs if i press a specific button and when i press the button again it stops and resumes if i press it again?

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Press Keystroke if Color on Screen is detected

Post by mikeyww » 04 Oct 2023, 10:43

Instead of Loop, you can use :arrow: SetTimer to create a timed loop. Your hotkey can then toggle the timer.

Mystarix
Posts: 5
Joined: 23 Mar 2023, 06:39

Re: Press Keystroke if Color on Screen is detected

Post by Mystarix » 04 Oct 2023, 10:45

mikeyww wrote:
04 Oct 2023, 10:43
Instead of Loop, you can use :arrow: SetTimer to create a timed loop. Your hotkey can then toggle the timer.
Hm... The timings are always different/random so i assume this wouldn't work.

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Press Keystroke if Color on Screen is detected

Post by mikeyww » 04 Oct 2023, 10:58

An alternative is below.

Code: Select all

#Requires AutoHotkey v2.0
#MaxThreadsPerHotkey 2
SoundBeep 1500

F3:: {
 Static on := False
 on := !on
 While on {
  Send 'e'
  Sleep 200
 }
 Reload
}

Post Reply

Return to “Gaming”