import time
from ctypes import *
import win32gui
import win32com.client as comclt
X = 119
Y = 53
def PILColorToRGB(pil_color):
""" convert a PIL-compatible integer into an (r, g, b) tuple """
hexstr = '%06x' % pil_color
# reverse byte order
r, g, b = hexstr[4:], hexstr[2:4], hexstr[:2]
r, g, b = [int(n, 16) for n in (r, g, b)]
return (r, g, b)
wsh = comclt.Dispatch("WScript.Shell")
w = win32gui
user = windll.LoadLibrary("c:\\windows\\system32\\user32.dll")
h = user.GetDC(0)
gdi = windll.LoadLibrary("c:\\windows\\system32\\gdi32.dll")
while True:
FG = w.GetWindowText(w.GetForegroundWindow()) #FG = Foreground window title.
if FG == "World of Warcraft":
rgb = (PILColorToRGB(gdi.GetPixel(h,X,Y))) #X, Y
time.sleep(0.333) #don't check too often.
if (rgb[0] >= 130): #While Pixel (X, Y) is Red...
#print "%d %d %d" % (rgb[0], rgb[1], rgb[2]) #Debug
wsh.SendKeys("{F12}") #Send a key.
time.sleep(0.7) #Add some extra down-time if we send the key.
else:
time.sleep(5)
Basically all this code does is read a pixel on the screen, and send a key (F12) if the pixel is red. But when using this code I regularly get some phantom key-code being pressed. The application I'm using this on is obviously world of warcraft, and I have checked that all keybinds are standard keybinds. However randomly it seems I get either an up arrow, or a w pressed, which moves my character forward whenever this code executes (F12 is bound to a macro, unbound from any movement. If I press f12 with a hardware event, it does not exhibit this behavior.
What in the world could be going on here?