![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,819
Rep Power: 5
![]() |
Optimizing my win32api function?
I have a program that is intended to record and playback arrow keys and space bar input exactly how they were pressed and at what time.
But I need it to be more precise/accurate/consistent, rather then just recording the number of empty loops in between each change... help please? main.py from win32api import *
from win32con import *
from time import sleep
def Record(filename):
save = []
done = False
lGate = False
rGate = False
dGate = False
uGate = False
sGate = False
b = -1
print 'Press Enter When Ready ...\nPress Escape To End'
while not GetAsyncKeyState(VK_RETURN):' '
while not done:
if GetAsyncKeyState(VK_LEFT):
if not lGate:
save.append(str(b)+'c') ; b = 0
lGate = True
save.append( "37a" )
elif lGate:
save.append(str(b)+'c') ; b = 0
lGate = False
save.append( "37b" )
if GetAsyncKeyState(VK_RIGHT):
if not rGate:
save.append(str(b)+'c') ; b = 0
rGate = True
save.append( "39a" )
elif rGate:
save.append(str(b)+'c') ; b = 0
rGate = False
save.append( "39b" )
if GetAsyncKeyState(VK_UP):
if not uGate:
save.append(str(b)+'c') ; b = 0
uGate = True
save.append( "38a" )
elif uGate:
save.append(str(b)+'c') ; b = 0
uGate = False
save.append( "38b" )
if GetAsyncKeyState(VK_DOWN):
if not dGate:
save.append(str(b)+'c') ; b = 0
dGate = True
save.append( "40a" )
elif dGate:
save.append(str(b)+'c') ; b = 0
dGate = False
save.append( "40b" )
if GetAsyncKeyState(VK_SPACE):
if not sGate:
save.append(str(b)+'c') ; b = 0
sGate = True
save.append( "32a" )
elif sGate:
save.append(str(b)+'c') ; b = 0
sGate = False
save.append( "32b" )
if GetAsyncKeyState(VK_ESCAPE):
done = True
if b + 1: b += 1
file = open(filename, 'w')
file.write( ''.join(save)[3:] )
file.close()
def Play(file, interval):
data = open(file, 'r').read()
r = []
a,o = 0,0
while a < len(data):
if data[a] == 'a':
r += ['keybd_event(%s, 0, KEYEVENTF_EXTENDEDKEY, 0)'%(data[o:a])]
o = a + 1
elif data[a] == 'b':
r += ['keybd_event(%s, 0, KEYEVENTF_KEYUP, 0)'%(data[o:a])]
o = a + 1
elif data[a] == 'c':
r += [ 'sleep( %s )' % ( float(int(data[o:a])) / float(interval)) ]
o = a + 1
a += 1
print 'Press Enter When Ready ...\nPress Escape To End'
while not GetAsyncKeyState(VK_RETURN):''
for event in r:
exec event
if GetAsyncKeyState(VK_ESCAPE): break
keybd_event(VK_LEFT, 0, KEYEVENTF_KEYUP, 0)
keybd_event(VK_RIGHT, 0, KEYEVENTF_KEYUP, 0)
keybd_event(VK_UP, 0, KEYEVENTF_KEYUP, 0) #reset keys
keybd_event(VK_DOWN, 0, KEYEVENTF_KEYUP, 0)
keybd_event(VK_SPACE, 0, KEYEVENTF_KEYUP, 0)
#Record('sample.sKey')
#Play('sample.sKey', 48500)
#shorter the interval = longer wait between movements (greater distance)
#bigger the interval = smaller wait between movements (lesser distance)sample.sKey Quote:
|
|
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,819
Rep Power: 5
![]() |
I have an idea. It times how long the loop took to execute, and then adds that amount to the delay (b) to occur before each key input. I can't find any good Pystat program though.
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,819
Rep Power: 5
![]() |
Err, okay I did it myself. But I still need help making it more accurate.
main.py import timeit
from win32api import *
from win32con import *
from time import sleep
def Record(filename):
save = []
done = False
lGate = False
rGate = False
dGate = False
uGate = False
sGate = False
print 'Press Spacebar When Ready ...\nPress Escape To End'
while not GetAsyncKeyState(VK_SPACE):' '
time = GetSystemTime()
b = time[-4]*60*60 + time[-3]*60 + float(time[-2]) + float(time[-1])/1000
def get_time(b):
time = GetSystemTime()
t = time[-4]*60*60 + time[-3]*60 + float(time[-2]) + float(time[-1])/1000
return t, str(t - b)
while not done:
if GetAsyncKeyState(VK_LEFT):
if not lGate:
lGate = True
b, x = get_time(b)
save.append( '%s:37a'%(x) )
elif lGate:
lGate = False
b, x = get_time(b)
save.append( '%s:37b'%(x) )
if GetAsyncKeyState(VK_RIGHT):
if not rGate:
rGate = True
b, x = get_time(b)
save.append( '%s:39a'%(x) )
elif rGate:
rGate = False
b, x = get_time(b)
save.append( '%s:39b'%(x) )
if GetAsyncKeyState(VK_UP):
if not uGate:
uGate = True
b, x = get_time(b)
save.append( '%s:38a'%(x) )
elif uGate:
uGate = False
b, x = get_time(b)
save.append( '%s:38b'%(x) )
if GetAsyncKeyState(VK_DOWN):
if not dGate:
dGate = True
b, x = get_time(b)
save.append( '%s:40a'%(x) )
elif dGate:
dGate = False
b, x = get_time(b)
save.append( '%s:40b'%(x) )
if GetAsyncKeyState(VK_SPACE):
if not sGate:
sGate = True
b, x = get_time(b)
save.append( '%s:32a'%(x) )
elif sGate:
sGate = False
b, x = get_time(b)
save.append( '%s:32b'%(x) )
if GetAsyncKeyState(VK_ESCAPE):
done = True
dat = '\n'.join(save)
file = open(filename, 'w')
file.write( dat )
file.close()
def Play(file, offset):
#offset /= 100
data = open(file, 'r').read()
r = []
a,o = 0,0
while a < len(data):
if data[a] == 'a':
for b in range(len(data[o:a])):
if data[o+b] == ':':
t = o+b
break
s = float(data[o:t])-offset
if s < 0:s=0
r += [ 'sleep(%s)'%(s) , 'keybd_event(%s, 0, KEYEVENTF_EXTENDEDKEY, 0)'%(data[t+1:a]) ]
o = a + 2
elif data[a] == 'b':
for b in range(len(data[o:a])):
if data[o+b] == ':':
t = o+b
break
s = float(data[o:t])-offset
if s < 0:s=0
r += [ 'sleep(%s)'%(s), 'keybd_event(%s, 0, KEYEVENTF_KEYUP, 0)'%(data[t+1:a]) ]
o = a + 2
a += 1
print 'Press Spacebar When Ready ...\nPress Escape To End'
while not GetAsyncKeyState(VK_SPACE):''
for event in r:
exec event
if GetAsyncKeyState(VK_ESCAPE): break
keybd_event(VK_LEFT, 0, KEYEVENTF_KEYUP, 0)
keybd_event(VK_RIGHT, 0, KEYEVENTF_KEYUP, 0)
keybd_event(VK_UP, 0, KEYEVENTF_KEYUP, 0) #reset keys
keybd_event(VK_DOWN, 0, KEYEVENTF_KEYUP, 0)
keybd_event(VK_SPACE, 0, KEYEVENTF_KEYUP, 0)
#Record('sample.sKey')
Play('sample.sKey', 0.0)sample.sKey 8.906:37a 0.156000000003:37b 0.312999999995:37a 0.0470000000059:37b 0.202999999994:37a 0.328000000009:37b 0.0469999999914:37a 0.125:37b 0.687000000005:37a 0.0:39a 0.0:37b 0.360000000001:39b 0.281000000003:37a 0.0:39a 0.0:37b 0.358999999997:39b 0.0:37a 0.0469999999914:37b 0.641000000003:39a 0.0:37a 0.266000000003:39b 0.125:39a 0.0149999999994:37b 0.735000000001:37a 0.0:39b 0.0:38a 0.0:40a 0.0:37b 0.0:38b 0.0779999999941:40b 0.0780000000086:39a 0.156000000003:39b 0.0159999999887:40a 0.139999999999:40b 0.0320000000065:38a 0.717999999993:37a 0.0:38b 0.0:40a 0.0:37b 0.0:40b 0.0:38a 0.172000000006:38b 0.0160000000033:40a 0.125:40b 0.0619999999908:39a 0.188000000009:39b 0.0:38a 0.296999999991:39a 0.0:38b 0.0310000000027:39b 0.0310000000027:40a 0.187999999995:39a 0.0:40b 0.0620000000054:39b 0.593999999997:38a 0.0:40a 0.0:39a 0.0:40b 0.0:39b 0.562000000005:37a 0.0:38b 0.0:40a 0.0:37b 0.0:40b 0.0469999999914:38a 0.219000000012:38b 0.0:40a 0.280999999988:39a 0.0:40b 0.0320000000065:39b 0.0149999999994:38a 0.141000000003:38b 0.0619999999908:39a 0.719000000012:37a 0.0:39b 0.0:38a 0.0:40a 0.0:37b 0.0:38b 0.671999999991:37a 0.0:38a 0.0:37b 0.0:40b 0.0160000000033:38b 0.0149999999994:40a 0.141000000003:40b 0.0619999999908:39a 0.157000000007:39b 0.0149999999994:40a 0.141000000003:40b 0.0939999999973:38a 0.108999999997:38b 0.0160000000033:37a 0.842999999993:38a 0.0:40a 0.0:38b 0.0:37b 0.0160000000033:40b 0.0780000000086:39a 0.905999999988:40a 0.0:38a 0.0:40b 0.0160000000033:38b 0.141000000003:39b 0.0149999999994:40a 0.172000000006:40b 0.0159999999887:38a 1.5:37a 0.0:39a 0.0:40a 0.0:37b 0.0:39b 0.0:38b 0.0149999999994:40b 0.0470000000059:38a 0.0629999999946:40a 0.0780000000086:38b 0.0469999999914:40b 0.0620000000054:38a 0.0939999999973:37a 0.0310000000027:38b 0.0939999999973:40a 0.0160000000033:37b 0.0620000000054:40b 0.0629999999946:40a 0.156000000003:37a 0.0:40b 0.0:37b 0.186999999991:39a 0.0160000000033:37a 0.108999999997:39b 0.0160000000033:37b 0.733999999997:39a 0.0160000000033:38a 0.0:39b 0.0:38b 0.968999999997:40a 0.0:37a 0.0:40b 0.0:37b 0.672000000006:38a 0.0149999999994:37a 0.0:38b 0.0:37b 0.0310000000027:39a 0.171999999991:39b 0.0160000000033:38a 0.0780000000086:38b 0.0939999999973:38a 0.0149999999994:39a 0.156999999992:38b 0.0:39b 0.828000000009:39a 0.0:40a 0.0:39b 0.0:40b 0.796999999991:38a 0.0:40a 0.0:37a 0.0:38b 0.0:40b 0.0:37b 1.35900000001:39a 0.0939999999973:37a 0.0:37b 0.0:39b 0.0939999999973:39a 0.0620000000054:39b 0.0629999999946:40a 0.125:40b 0.0620000000054:40a 0.702999999994:39a 0.0:38a 0.0:40b 0.0:39b 0.0:38b 0.0160000000033:38a 0.108999999997:38b 0.0939999999973:39a 0.125:39b 0.0470000000059:40a 0.139999999999:40b 1.375:39a 0.0:38a 0.0:40a 0.0:37a 0.0:39b 0.0:38b 0.0160000000033:37b |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Nice monologue; watch out Leno
.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|