#-*- coding: gbk -*-
import os,sys
import time
import ctypes
import win32gui
import win32api
import win32con
# pywin32-214.win32-py2.6.exe
MapVirtualKey = ctypes.windll.user32.MapVirtualKeyA
keymap = { '1':49, '2':50, '3':51, '4':52, '5':53, '6':54, '7':55, '8':56, '9':57, '0':48 }
def GetForegroundWindowName():
HWND = win32gui.GetForegroundWindow()
return win32gui.GetWindowText(HWND)
if __name__ == '__main__':
ks = ['1', '2', '3', '4', '5']
ks_p = [(keymap[k], MapVirtualKey(keymap[k],0)) for k in ks]
#ks_p= [(49, 2), (50, 3), (51, 4), (52, 5), (53, 6)]
print '*** If you get Game window in your top window, this script will get to run!!'
i = 0
while True:
print 'I waiting top window...'
fg = GetForegroundWindowName()
if fg != 'MyGame':
time.sleep(0.5)
continue
print 'Do actions...'
vkey, scan = ks_p[i]
win32api.keybd_event(vkey, scan, 0, 0)
time.sleep(0.5)
win32api.keybd_event(vkey, scan, win32con.KEYEVENTF_KEYUP, 0)
i += 1
if i == len(ks):
i = 0
|