-1
import time
import keyboard
import pickle

while True:
    print("Connected")
    while True:  #start
    if keyboard.is_pressed('F3'):
        print("Start")
        keyboard.start_recording()
        break
while True: #end
    if keyboard.is_pressed('F4'):
        print("End")
        events = keyboard.stop_recording()
        profile_file = open("profile.pickle", "wb")
        profile = (events)
        pickle.dump(events, profile_file)
        profile_file.close()
        break
while True: #Play
    if keyboard.is_pressed('F5'):
        print("Play")
        keyboard.replay(events)
        print(events)    
        print("Finish")
        break

break    

I made a keyboard recording code with Python.

When I print out the recording, it only prints out the key-down key-up.

[KeyboardEvent(f3 up), KeyboardEvent(s down), KeyboardEvent(s up), KeyboardEvent(d down), KeyboardEvent(f down), KeyboardEvent(d up), KeyboardEvent(f up), KeyboardEvent(f4 down)]

All I want is to know the delay between those values. Is that possible?

Kangroo
  • 1
  • 1
  • 1
    please add the code you alraedy have, so I can help solve your problem – TERMINATOR Nov 26 '20 at 07:57
  • Are you familiar with the [`time`](https://docs.python.org/3/library/time.html) module? It can be used to measure times – Tomerikoo Nov 26 '20 at 08:26
  • Does this answer your question? [Measuring time between keystrokes in python](https://stackoverflow.com/questions/9133923/measuring-time-between-keystrokes-in-python) – Tomerikoo Nov 26 '20 at 08:35

2 Answers2

0

It would be helpful if you could post some code and your desired output, but if you just want a recording of time between two inputs:

import time
timer = 0

# input key down:
    # while input is NOT key up:
        time.sleep(0.1)
        timer += 0.1

I don't know your code or outcome, so I'm not writing the if and while loops.

lrainey6-eng
  • 183
  • 1
  • 1
  • 8
  • Why the `sleep`? That seems to temper the measurement... – Tomerikoo Nov 26 '20 at 08:23
  • you use `time.sleep(seconds)` to wait for a set time period – lrainey6-eng Nov 26 '20 at 13:08
  • Yes I'm aware. But if trying to measure time between button presses, why block the program waiting? – Tomerikoo Nov 26 '20 at 13:18
  • well this was just an example; you could decrease the waiting time to something like `0.01`, or you could create a boolean (e.g. `is_key_pressed`) and if it's true, run this, which means you can continue with other lines of code during the `if` statement, and when the next key is pressed, set the boolean to `False` and the timer will stop adding. – lrainey6-eng Nov 26 '20 at 19:38
0

You may use the time.time() function to get the current time in seconds as a float (with some additional precision after the decimal point) when your key is pressed and released, then subtract the two values to get the duration. For instance:

import time

# [keydown code]
pressed_time = time.time()

# [keyup code]
released_time = time.time()

duration = released_time - pressed_time

Please note that if the system time changes between the two measurements, the duration value could be negative.

Altareos
  • 843
  • 6
  • 13