/
CPKeyBoardSimulator.py
108 lines (89 loc) · 3.09 KB
/
CPKeyBoardSimulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# CP: cross platform
# using pyautogui lib to implement keyboard sender
import pyautogui
import time
# screenWidth, screenHeight = pyautogui.size()
# currentMouseX, currentMouseY = pyautogui.position()
# pyautogui.typewrite('Hello world!', interval=0.25) # type with quarter-second pause in between each key
# pyautogui.keyDown('shift')
# pyautogui.typewrite('under shift...', interval=0.25)
# pyautogui.typewrite(['left', 'left', 'left', 'left', 'left', 'left'])
# pyautogui.keyUp('shift')
# pyautogui.keyDown('a')
# time.sleep( 0.13 )
# pyautogui.keyUp('a')
class CPKeyBoardSimulator:
def __init__( self, pressKeySec = None, gap2KeySec = None, doTimeStamp = True ):
if pressKeySec is not None:
self._pressKeySec = pressKeySec
else:
self._pressKeySec = 0.13
if gap2KeySec is not None:
self._gap2KeySec = gap2KeySec
else:
self._gap2KeySec = 0.11
self.doTimeStamp = doTimeStamp
def SendKey( self, aKey ):
if self.doTimeStamp:
print( "press %5s @ %15.3f" % (aKey, time.time()) )
pyautogui.keyDown(aKey)
if self.doTimeStamp:
print( "press %5s @ %15.3f" % (aKey, time.time()) )
time.sleep( self._pressKeySec )
if self.doTimeStamp:
print( "upkey %5s @ %15.3f" % (aKey, time.time()) )
pyautogui.keyUp(aKey)
if self.doTimeStamp:
print( "upkey %5s @ %15.3f" % (aKey, time.time()) )
time.sleep( self._gap2KeySec )
def printKey( self, aKey ):
keyList = ['x', 'z', 'c', 'space', 'right', 'left']
keyName = [ "X", "Z", "C", "Space", "Right", "Left" ]
print(keyName[keyList.index(aKey)])
def RotateRight( self ):
self.SendKey('x')
def RotateLeft( self ):
self.SendKey('z')
def HoldBlock( self ):
self.SendKey('c')
def FallInstantly( self ):
self.SendKey('space')
def MoveRight( self ):
self.SendKey('right')
def MoveLeft( self ):
self.SendKey('left')
def MoveLeftMulti( self, count ):
for i in range( count ):
self.MoveLeft()
def MoveRightMulti( self, count ):
for i in range( count ):
self.MoveRight()
def RotateRightMulti( self, count ):
if count < 3:
for i in range( count ):
self.RotateRight()
else: # count == 3
self.RotateLeft()
if __name__ == '__main__':
time.sleep(3)
"""
"X CZC"
keyBoard = KeyBoardSimulator()
keyBoard.RotateRight()
keyBoard.SendKey( 0x20 )
keyBoard.RotateLeft()
keyBoard.MoveLeft()
keyBoard.HoldBlock()
keyBoard.MoveRight()
keyBoard.HoldBlock()
"""
keyBoard = CPKeyBoardSimulator(0,0)
cmdOrder = [keyBoard.MoveLeft, keyBoard.MoveLeft, keyBoard.MoveLeft, keyBoard.FallInstantly]
#cmdOrder = [ keyBoard.FallInstantly ]
for i in range(20):
#keyBoard.MoveLeftMulti(3)
#keyBoard.RotateRightMulti(2)
keyBoard.MoveRightMulti(4)
keyBoard.FallInstantly()
keyBoard.MoveLeftMulti(3)
keyBoard.FallInstantly()