Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38827 > unrolled thread
| Started by | DaGeek247 <imageek247@gmail.com> |
|---|---|
| First post | 2013-02-13 11:47 -0800 |
| Last post | 2013-02-14 11:04 -0500 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
Simulate Keyboard keypress Delay DaGeek247 <imageek247@gmail.com> - 2013-02-13 11:47 -0800
Re: Simulate Keyboard keypress Delay Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-02-13 18:39 -0500
Re: Simulate Keyboard keypress Delay 88888 Dihedral <dihedral88888@googlemail.com> - 2013-02-13 16:45 -0800
Re: Simulate Keyboard keypress Delay inq1ltd <inq1ltd@inqvista.com> - 2013-02-14 11:04 -0500
| From | DaGeek247 <imageek247@gmail.com> |
|---|---|
| Date | 2013-02-13 11:47 -0800 |
| Subject | Simulate Keyboard keypress Delay |
| Message-ID | <79a6ab1c-d95e-45d9-ba0c-39fa68755e8c@googlegroups.com> |
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?
[toc] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2013-02-13 18:39 -0500 |
| Message-ID | <mailman.1751.1360798778.2939.python-list@python.org> |
| In reply to | #38827 |
On Wed, 13 Feb 2013 11:47:36 -0800 (PST), DaGeek247
<imageek247@gmail.com> declaimed the following in
gmane.comp.python.general:
> 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%28v=vs.85%29.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;
<snip>
> 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;
<snip>
> 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.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2013-02-13 16:45 -0800 |
| Message-ID | <beb55b75-b796-4a7a-aa9e-7588abf14c6c@googlegroups.com> |
| In reply to | #38827 |
DaGeek247於 2013年2月14日星期四UTC+8上午3時47分36秒寫道: > 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? I believe you can use the raw_input function in python. But loop through strings ended by \r\n or \r.
[toc] | [prev] | [next] | [standalone]
| From | inq1ltd <inq1ltd@inqvista.com> |
|---|---|
| Date | 2013-02-14 11:04 -0500 |
| Message-ID | <mailman.1770.1360860014.2939.python-list@python.org> |
| In reply to | #38827 |
[Multipart message — attachments visible in raw view] — view raw
On Wednesday, February 13, 2013 11:47:36 AM DaGeek247 wrote:
> 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?
this will only work on a windows machine. It is from C++ runtime lib
msvcrt.dll
in py module
import msvcrt
while 1:
ch = msvcrt.getch() ## returns one char
if ch == 'Y' :
# do stuff or break
break
print "%d (%r)" % (ch, ch)
# also, kbhit() # returns true if char is available
# also, ungetch(ch) # undo char ch
jd
inqvista.com
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web