Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 13th, 2013, 01:59 PM
DaGeek247 DaGeek247 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Location: The Aether
Posts: 3 DaGeek247 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 3 sec
Reputation Power: 0
Send a message via Skype to DaGeek247
Keyboard Keypress Record Help

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

Code:
#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;

Code:
#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;

Code:
#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 key presses, and missing others. Does anybody have any suggestions? Any and all help is greatly appreciated.

Reply With Quote
  #2  
Old February 13th, 2013, 03:47 PM
DaGeek247 DaGeek247 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Location: The Aether
Posts: 3 DaGeek247 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 3 sec
Reputation Power: 0
Send a message via Skype to DaGeek247
Actuall Code

Here are the results of my best efforts to get this to work, using the above examples as the base.

Code:
import win32api 
import time
print("Everything Imported Successfully")

def loop(OldKeyChar,PrevTime):
    while(True): #always checking
        time.sleep(0.02)
        for num in range(0,256):#iterate through list of ascii codes
            if win32api.GetAsyncKeyState(num): #if ascii code key is being pressed 
                if OldKeyChar == num: #if key is being repeated
                    #if repeated key is being pressed after amount of time
                    if PrevTime < (time.time() - .5):
                        #do stuff because key has been pressed twice
                        print(chr(num ) + " Key " + str(num) + " has been pressed repeatedly")
                        PrevTime = time.time()
                    #else: 
                        #don't do stuff because key is pressed to soon
                        #print(chr(num ) + " Key " + str(num) + " has been held down")
                        
                else: 
                    #do stuff because key is new
                    PrevTime = time.time()
                    print(chr(num ) + " Key " + str(num) + " has been pressed")
                    OldKeyChar = num
temp = time.time()
loop(OldKeyChar="",PrevTime=temp)


It works, but not perfectly, because words like 'look' fail when I type too quickly, and if type to slowly, the keys are repeated when they should not be. surely there is a better way?

Reply With Quote
  #3  
Old February 13th, 2013, 05:52 PM
DaGeek247 DaGeek247 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Location: The Aether
Posts: 3 DaGeek247 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 3 sec
Reputation Power: 0
Send a message via Skype to DaGeek247
screw timing, I fixed this a different way.

basically It checks if a key is being pressed, then stores that key in a variable. it checks again, and if that key is no longer being pressed then it records. here's the code for you goobers out there wanting to know.

Code:
import win32api 
print("Everything Imported Successfully")

def loop(OldKeyChar,defi):
    while(True): #always checking
        for num in range(0,256):#iterate through list of ascii codes
            if not win32api.GetAsyncKeyState(num): #if key is not being pressed
                if defi == "1": #make sure that this isn't repeated
                    if OldKeyChar == num: #check to see if 
                        print(chr(num ) + " Key " + str(num) + " has been unpressed")
                        defi="0"
            else:
                print(chr(num ) + " Key " + str(num) + " has been pressed")
                OldKeyChar = num
                defi="1"

print("Starting loop")
loop(OldKeyChar="",defi="0")

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Keyboard Keypress Record Help

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap