Created
May 3, 2017 07:20
-
-
Save tmt514/e8ecf6926369f0c483de2c4a80de25df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from play import KeyUp, KeyDown, KeyPress | |
from time import sleep | |
import numpy as np | |
import cv2 | |
from mss import mss | |
from PIL import Image | |
from Quartz.CoreGraphics import CGEventCreateKeyboardEvent | |
from Quartz.CoreGraphics import CGEventPost | |
# Python releases things automatically, using CFRelease will result in a scary error | |
#from Quartz.CoreGraphics import CFRelease | |
from Quartz.CoreGraphics import kCGHIDEventTap, kCGAnnotatedSessionEventTap | |
mon = {'top': 410, 'left': 160, 'width': 900, 'height': 200} | |
sct = mss() | |
#KeyDown('left') | |
#KeyDown('right') | |
last_red_left = 0 | |
last_red_right = 0 | |
cnt = 0 | |
grace_period = 4 | |
cons_period = 200 | |
chance = 0 | |
cons = 0 | |
XX = kCGHIDEventTap | |
l = None | |
r = None | |
keyUpLeftEvent = CGEventCreateKeyboardEvent(None, 0x7B, False) | |
keyDownLeftEvent = CGEventCreateKeyboardEvent(None, 0x7B, True) | |
keyUpRightEvent = CGEventCreateKeyboardEvent(None, 0x7C, False) | |
keyDownRightEvent = CGEventCreateKeyboardEvent(None, 0x7C, True) | |
while True: | |
cnt += 1 | |
sct.get_pixels(mon) | |
img_orig = Image.frombytes('RGB', (sct.width, sct.height), sct.image) | |
img = np.array(img_orig) | |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | |
del img_orig | |
R = 10 | |
Ax = 265 | |
Ay = 245 | |
Bx = 485 | |
By = 245 | |
for Ax in range(240, 291, 10): | |
for Ay in range(230, 261, 10): | |
ACx = Ax+R//2 | |
ACy = Ay+R//2 | |
cleft_R = img[ACy-5:ACy+5,ACx-5:ACx+5,2].sum() // 100 | |
cleft_G = img[ACy-5:ACy+5,ACx-5:ACx+5,1].sum() // 100 | |
cleft_B = img[ACy-5:ACy+5,ACx-5:ACx+5,0].sum() // 100 | |
if cleft_R >= cleft_B + cleft_G + 100 and cleft_B <= 60: | |
last_red_left = cnt | |
for Bx in range(460, 511, 10): | |
for By in range(230, 261, 10): | |
BCx = Bx+R//2 | |
BCy = By+R//2 | |
cright_R = img[BCy-5:BCy+5,BCx-5:BCx+5,2].sum() // 100 | |
cright_G = img[BCy-5:BCy+5,BCx-5:BCx+5,1].sum() // 100 | |
cright_B = img[BCy-5:BCy+5,BCx-5:BCx+5,0].sum() // 100 | |
if cright_R >= cright_B + cright_G + 100 and cright_B <= 60: | |
last_red_right = cnt | |
left_release = (last_red_left + grace_period >= cnt) | |
right_release = (last_red_right + grace_period >= cnt) | |
colorL = (0, 255, 0) | |
if left_release: | |
colorL = (0, 0, 255) | |
colorR = (0, 255, 0) | |
if right_release: | |
colorR = (0, 0, 255) | |
for Ax in range(240, 291, 10): | |
for Ay in range(230, 261, 10): | |
cv2.rectangle(img, (Ax, Ay), (Ax+R, Ay+R), colorL, 3) | |
for Bx in range(460, 511, 10): | |
for By in range(230, 261, 10): | |
cv2.rectangle(img, (Bx, By), (Bx+R, By+R), colorR, 3) | |
if last_red_left == cnt or last_red_right == cnt: | |
cv2.imshow('test', img) | |
cleft = (cleft_R, cleft_B) | |
cright = (cright_R, cright_B) | |
print(chance, cleft, cright, cnt, l, left_release, r, right_release) | |
#l = not left_release | |
#r = not right_release | |
cons += 1 | |
if cons >= cons_period: | |
print("RESET>>>>>>>>>>>>>>>>>>>>>>>>") | |
cons = 0 | |
if not l: | |
CGEventPost(XX, CGEventCreateKeyboardEvent(None, 0x7B, False)) | |
CGEventPost(XX, CGEventCreateKeyboardEvent(None, 0x7B, True)) | |
else: | |
CGEventPost(XX, CGEventCreateKeyboardEvent(None, 0x7B, True)) | |
CGEventPost(XX, CGEventCreateKeyboardEvent(None, 0x7B, False)) | |
if not r: | |
CGEventPost(XX, CGEventCreateKeyboardEvent(None, 0x7C, False)) | |
CGEventPost(XX, CGEventCreateKeyboardEvent(None, 0x7C, True)) | |
else: | |
CGEventPost(XX, CGEventCreateKeyboardEvent(None, 0x7C, True)) | |
CGEventPost(XX, CGEventCreateKeyboardEvent(None, 0x7C, False)) | |
cv2.waitKey(1) | |
if l != left_release: | |
if left_release: | |
CGEventPost(XX, keyUpLeftEvent) | |
else: | |
CGEventPost(XX, keyDownLeftEvent) | |
l = left_release | |
cons = 0 | |
if r != right_release: | |
if right_release: | |
CGEventPost(XX, keyUpRightEvent) | |
else: | |
CGEventPost(XX, keyDownRightEvent) | |
r = right_release | |
cons = 0 | |
if cv2.waitKey(5) & 0xFF == ord('q'): | |
cv2.destroyAllWindows() | |
break | |
KeyUp('left') | |
KeyUp('right') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import time | |
from Quartz.CoreGraphics import CGEventCreateKeyboardEvent | |
from Quartz.CoreGraphics import CGEventPost | |
# Python releases things automatically, using CFRelease will result in a scary error | |
#from Quartz.CoreGraphics import CFRelease | |
from Quartz.CoreGraphics import kCGHIDEventTap | |
# From http://stackoverflow.com/questions/281133/controlling-the-mouse-from-python-in-os-x | |
# and from https://developer.apple.com/library/mac/documentation/Carbon/Reference/QuartzEventServicesRef/index.html#//apple_ref/c/func/CGEventCreateKeyboardEvent | |
def KeyDown(k): | |
keyCode, shiftKey = toKeyCode(k) | |
#time.sleep(0.0001) | |
if shiftKey: | |
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, True)) | |
time.sleep(0.0001) | |
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, True)) | |
time.sleep(0.0001) | |
if shiftKey: | |
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, False)) | |
time.sleep(0.0001) | |
def KeyUp(k): | |
keyCode, shiftKey = toKeyCode(k) | |
#time.sleep(0.0001) | |
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, False)) | |
time.sleep(0.0001) | |
def KeyPress(k): | |
keyCode, shiftKey = toKeyCode(k) | |
time.sleep(0.0001) | |
if shiftKey: | |
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, True)) | |
time.sleep(0.0001) | |
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, True)) | |
time.sleep(0.0001) | |
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, False)) | |
time.sleep(0.0001) | |
if shiftKey: | |
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, False)) | |
time.sleep(0.0001) | |
# From http://stackoverflow.com/questions/3202629/where-can-i-find-a-list-of-mac-virtual-key-codes | |
def toKeyCode(c): | |
shiftKey = False | |
# Letter | |
if c.isalpha(): | |
if not c.islower(): | |
shiftKey = True | |
c = c.lower() | |
if c in shiftChars: | |
shiftKey = True | |
c = shiftChars[c] | |
if c in keyCodeMap: | |
keyCode = keyCodeMap[c] | |
else: | |
keyCode = ord(c) | |
return keyCode, shiftKey | |
shiftChars = { | |
'~': '`', | |
'!': '1', | |
'@': '2', | |
'#': '3', | |
'$': '4', | |
'%': '5', | |
'^': '6', | |
'&': '7', | |
'*': '8', | |
'(': '9', | |
')': '0', | |
'_': '-', | |
'+': '=', | |
'{': '[', | |
'}': ']', | |
'|': '\\', | |
':': ';', | |
'"': '\'', | |
'<': ',', | |
'>': '.', | |
'?': '/' | |
} | |
keyCodeMap = { | |
'a' : 0x00, | |
's' : 0x01, | |
'd' : 0x02, | |
'f' : 0x03, | |
'h' : 0x04, | |
'g' : 0x05, | |
'z' : 0x06, | |
'x' : 0x07, | |
'c' : 0x08, | |
'v' : 0x09, | |
'b' : 0x0B, | |
'q' : 0x0C, | |
'w' : 0x0D, | |
'e' : 0x0E, | |
'r' : 0x0F, | |
'y' : 0x10, | |
't' : 0x11, | |
'1' : 0x12, | |
'2' : 0x13, | |
'3' : 0x14, | |
'4' : 0x15, | |
'6' : 0x16, | |
'5' : 0x17, | |
'=' : 0x18, | |
'9' : 0x19, | |
'7' : 0x1A, | |
'-' : 0x1B, | |
'8' : 0x1C, | |
'0' : 0x1D, | |
']' : 0x1E, | |
'o' : 0x1F, | |
'u' : 0x20, | |
'[' : 0x21, | |
'i' : 0x22, | |
'p' : 0x23, | |
'l' : 0x25, | |
'j' : 0x26, | |
'\'' : 0x27, | |
'k' : 0x28, | |
';' : 0x29, | |
'\\' : 0x2A, | |
',' : 0x2B, | |
'/' : 0x2C, | |
'n' : 0x2D, | |
'm' : 0x2E, | |
'.' : 0x2F, | |
'`' : 0x32, | |
'k.' : 0x41, | |
'k*' : 0x43, | |
'k+' : 0x45, | |
'kclear' : 0x47, | |
'k/' : 0x4B, | |
'k\n' : 0x4C, | |
'k-' : 0x4E, | |
'k=' : 0x51, | |
'k0' : 0x52, | |
'k1' : 0x53, | |
'k2' : 0x54, | |
'k3' : 0x55, | |
'k4' : 0x56, | |
'k5' : 0x57, | |
'k6' : 0x58, | |
'k7' : 0x59, | |
'k8' : 0x5B, | |
'k9' : 0x5C, | |
# keycodes for keys that are independent of keyboard layout | |
'\n' : 0x24, | |
'\t' : 0x30, | |
' ' : 0x31, | |
'del' : 0x33, | |
'delete' : 0x33, | |
'esc' : 0x35, | |
'escape' : 0x35, | |
'cmd' : 0x37, | |
'command' : 0x37, | |
'shift' : 0x38, | |
'caps lock' : 0x39, | |
'option' : 0x3A, | |
'ctrl' : 0x3B, | |
'control' : 0x3B, | |
'right shift' : 0x3C, | |
'rshift' : 0x3C, | |
'right option' : 0x3D, | |
'roption' : 0x3D, | |
'right control' : 0x3E, | |
'rcontrol' : 0x3E, | |
'fun' : 0x3F, | |
'function' : 0x3F, | |
'f17' : 0x40, | |
'volume up' : 0x48, | |
'volume down' : 0x49, | |
'mute' : 0x4A, | |
'f18' : 0x4F, | |
'f19' : 0x50, | |
'f20' : 0x5A, | |
'f5' : 0x60, | |
'f6' : 0x61, | |
'f7' : 0x62, | |
'f3' : 0x63, | |
'f8' : 0x64, | |
'f9' : 0x65, | |
'f11' : 0x67, | |
'f13' : 0x69, | |
'f16' : 0x6A, | |
'f14' : 0x6B, | |
'f10' : 0x6D, | |
'f12' : 0x6F, | |
'f15' : 0x71, | |
'help' : 0x72, | |
'home' : 0x73, | |
'pgup' : 0x74, | |
'page up' : 0x74, | |
'forward delete' : 0x75, | |
'f4' : 0x76, | |
'end' : 0x77, | |
'f2' : 0x78, | |
'page down' : 0x79, | |
'pgdn' : 0x79, | |
'f1' : 0x7A, | |
'left' : 0x7B, | |
'right' : 0x7C, | |
'down' : 0x7D, | |
'up' : 0x7E | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, it looks like these are some code to play a game on mac, by grabbing screenshots to make decisions, and pressing keyboard keys to control the game. I have tried using this exact method, and the keyboard presses will work when I switch the active window to a text editor, but not in OpenEmu games. Which game does this control?