Press Key When Pixel Change Colour / AutoHotkey to KM Translation / Game Healing Bot

Hi

I am new into Keyboard Maestro, so far I was using AutoHotkey to execute few simple scripts in 2D game called 'Tibia' on Windows.

Basically what I need is a macro which will watch pixel location on the screen while the game is on, and then if the pixel get specific colour it will press the key. However if the specific colour is keep being on the screen, the macro needs to keep pressing the key but with small pauses (0.5 sec for example).
I will appreciate it if someone could help me/give me some tips how to make it work on Keyboard Maestro.

Below the AutoHotkey scripts for your reference. First one is checking the mouse location (x, y) and colour ID. The other two work just as I describe it above.

Script1:

z::  ; Control+Alt+Z hotkey.
MouseGetPos, MouseX, MouseY
PixelGetColor, color, %MouseX%, %MouseY%
MsgBox The color at the current cursor position is %color% x= %MouseX% y= %MouseY% .
return

Script2:

Loop,
{        
    If WinActive("Tibia")
    {
        PixelGetColor, Check40, 1215, 219
        If (Check40 == 0x423F3C){
            SendInput {F3}
            Sleep, 200
        }
    }
    Sleep, 50
}

Script3:

#SingleInstance, Force
SetKeyDelay,0
x75 := 1299
HealthY := 286
Loop,
{
	If WinActive("Tibia")
	{
		PixelGetColor, Check75, %x75%, %HealthY%
		If (Check75 != 0x4F4FDB)
			Send, {F5}

	}
	Sleep, 400
}
Return

Thank you for your help!!

You mentioned Windows, but I presume your current problem is on macOS.

I would love to write a macro, but I have an appointment in 5 minutes, so all I can do at this moment is describe the solution.

The solution is to write a macro which runs in an infinite loop. It checks the pixel at the address you want, and if the colour matches, it clicks there, followed by a pause for 0.5 seconds.

After I get back from my appointment, if you still need help, and nobody has helped, I'll write the code.

Hi Sleepy!

Thanks for your answer! Yes, that’s right I am currently using MacOS with m1 chip.
I will be grateful if you could do that for me! The main thing it need to press the key for example „f3” when the pixel get specify colour.

Thank you!

image

Well, I had 3 minutes to write the above code for you, but it won't run until you modify it with the right pixel address and the correct value for the amount of red at that pixel. I have to run off again for an hour. Either you can try working with this, or you can wait until I come back to explain how you can get the pixel address(*) and the value for the amount of red. If you can figure those things out yourself, that's great.

EDIT: one of the easiest ways to get the pixel address AND at the same time the pixel's RGB values are by running the macOS utility called "Digital Color Meter." If you fetch the value of the pixel at that location using the PIXEL(Red,x,y) function you can see the value of Red that it returns for that pixel. It doesn't matter whether you use Red, Green or Blue. Does this clarify how you get these values?

1 Like

Every day I write macros using KM that does similar things to what you are doing. But from your description I think your approach has complexity that you really don't need. In other words, I think you should try a different approach.

I.e., I don't think you should be looking at pixels at all. Pixels are hard to deal with and for various reasons may not reliably work. A more reliable method is to use the KM action Find Image.

I'll support you whichever way you want to go, but I've done this hundreds (nay, thousands) of times and I know from experience how powerful the Find Image action is. In fact, recently, I've been able to replace many of my Find Image actions using the more powerful Monterey OCR capabilities. Maybe we should be considering that!

Of course, since I can't see your screen, I can't give very good advice on how to use the Find Image action to get the job done. So I'll just assume that you want to use the more difficult PIXEL() function for now. I'll reconsider my last message about using PIXEL() and may provide you with more advice.

I know its been a while since you wrote this, but I just saw this.

Is the PIXEL calculation thing in keyboard maestro continually checking the pixel? Also, is it possible to have it detect any colour at some specific location on the screen? or are they predefined to red,blue,green etc?

Have a look at the KM wiki function:PIXEL [Keyboard Maestro Wiki]

It's checked when you execute the action.

I'm taking a break from the forums, but you sent me this message and I would like to see if I can reply to you this way.

No, the PIXEL function is not continually checking the pixel. It checks only when you execute the function.

Yes, it is possible to detect any colour at some specific location on the screen. The PIXEL function can do that. But you have to go to some extra work. What colour are you trying to detect? Each colour that you can see is defined as a combination of Red, Green and Blue values. If you know the R/B/G values of the colour you are trying to detect, you can make three calls to PIXEL() to check those three component values.

While it can be done, I personally wouldn't put this into a loop, since the PIXEL() function feels a little slow to me. You may want to start a new forum topic and describe your problem there.

-Sleepy

Thanks for the replies both of you.

It seems like Keyboard Maestro isn't really the way to go for me then, as what I'm trying to do is do a bunch of actions based on what color a pixel is (and there's about 20 different colors).