Simulate Keyboard keypress Delay

D

DaGeek247

I am using the windows api feature getasynckeystate() to check the status of every key pressed; like this;

#always checking
while(True):
#iterate through list of ascii codes
for num in range(0,127):
#if ascii code key is being pressed
if win32api.GetAsyncKeyState(num):
#do stuff

This works great, almost. The issue that comes up now is that every time i press a key, the code grabs two or three key presses.

So i tried making sure that repeated keys weren't pressed repeatedly;

#always checking
while(True):
#iterate through list of ascii codes
for num in range(0,127):
#if ascii code key is being pressed
if win32api.GetAsyncKeyState(num):
if oldkeychar == num:
#don't do stuff
else:
#do stuff

this works great, but It won't record stuff like 'look' or 'suffer' because it doesn't record repeated keys. So I try doing a delay instead;

#always checking
while(True):
#iterate through list of ascii codes
for num in range(0,127):
#if ascii code key is being pressed
if win32api.GetAsyncKeyState(num):
if oldkeychar == num:
if crrenttime > (time.time() - .5)
#do stuff because key has been repeated, but not because it was held down
else:
#don't do stuff because key is pressed to soon
else:
#do stuff because key is not repeated
currenttime = time.time()

this almost works, but I end recording some double keypresses, and missing others. Does anybody have any suggestions?
 
D

Dennis Lee Bieber

I am using the windows api feature getasynckeystate() to check the status of every key pressed; like this;

#always checking
while(True):
#iterate through list of ascii codes
for num in range(0,127):
#if ascii code key is being pressed
if win32api.GetAsyncKeyState(num):
#do stuff
Wouldn't GetKeyboardState
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646299(v=vs.85).aspx
work better? Grab all the keys at once into an array, then scan the
array for status...
This works great, almost. The issue that comes up now is that every time i press a key, the code grabs two or three key presses.

So i tried making sure that repeated keys weren't pressed repeatedly;

this works great, but It won't record stuff like 'look' or 'suffer' because it doesn't record repeated keys. So I try doing a delay instead;

this almost works, but I end recording some double keypresses, and missing others. Does anybody have any suggestions?

Off hand, the simplest scheme I could think of is, when you detect a
key press, you loop over it until you detect the key release -- only
then do you start scanning for other keys.

IOWs, you should not be triggering on the state itself, but on the
change of state in both directions.
 
8

88888 Dihedral

DaGeek247æ–¼ 2013å¹´2月14日星期四UTC+8上åˆ3時47分36秒寫é“:
I am using the windows api feature getasynckeystate() to check the statusof every key pressed; like this;



#always checking

while(True):

#iterate through list of ascii codes

for num in range(0,127):

#if ascii code key is being pressed

if win32api.GetAsyncKeyState(num):

#do stuff



This works great, almost. The issue that comes up now is that every time i press a key, the code grabs two or three key presses.



So i tried making sure that repeated keys weren't pressed repeatedly;



#always checking

while(True):

#iterate through list of ascii codes

for num in range(0,127):

#if ascii code key is being pressed

if win32api.GetAsyncKeyState(num):

if oldkeychar == num:

#don't do stuff

else:

#do stuff



this works great, but It won't record stuff like 'look' or 'suffer' because it doesn't record repeated keys. So I try doing a delay instead;



#always checking

while(True):

#iterate through list of ascii codes

for num in range(0,127):

#if ascii code key is being pressed

if win32api.GetAsyncKeyState(num):

if oldkeychar == num:

if crrenttime > (time.time() - .5)

#do stuff because key has been repeated, but not because it was held down

else:

#don't do stuff because key is pressed to soon

else:

#do stuff because key is not repeated

currenttime = time.time()



this almost works, but I end recording some double keypresses, and missing others. Does anybody have any suggestions?

I believe you can use the raw_input function in python.

But loop through strings ended by \r\n or \r.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top