#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    The Aether
    Posts
    3
    Rep Power
    0

    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.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    The Aether
    Posts
    3
    Rep Power
    0

    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?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    The Aether
    Posts
    3
    Rep Power
    0
    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")

IMN logo majestic logo threadwatch logo seochat tools logo