0

I am trying to make a typing speed tester. The program takes a line from a file that contains words and prints it in the console then it asks the user to type in the printed line to check how fast the user types. But I want to start the timer function only when the user hits the first key and then end it when he presses enter key after typing the whole line.

print(line)
start = time()
entered_line = input()
end = time()
total_time += end - start

The above piece of code is enclosed in a for loop above it. What I have been able to do as I am a beginner is that I start the time immediately after the line from the file is printed on the screen that results in error if the user doesn't start typing immediately. So an easy and simple solution will be appreciated. Thanks!

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Does this answer your question? [Measuring time between keystrokes in python](https://stackoverflow.com/questions/9133923/measuring-time-between-keystrokes-in-python) – Tomerikoo Sep 16 '20 at 11:22
  • No, it didn't. I tried to use msvcrt.kbhit() but i can't use it they way i want it to as it will require an if statement which i cant fit in to --> entered_line = input(), line. – Haris Khan Sep 16 '20 at 11:58

1 Answers1

1

I think this can get you what you want

import time
from getkey import getkey
line = "here is the line you are trying to type"
print(line)
print("you can start by hitting any key")
print("hit ENTER when you are done")
k = getkey()
start_time = time.time()
your_input = str(k) + input("you can start now\n\n" + str(k))
end_time = time.time()
print("time elapsed =", end_time-start_time, "seconds")
print("the status of the line you just typed =", line == your_input)
Shoaib Mirzaei
  • 512
  • 4
  • 11
  • @rajah9, I just run it again and it worked fine. I even waited 30 second and it did not add it to the elapsed time. you know in the `7th line (pressing a key)` the program waits until a key is pressed, until then, the `8th line (timer's start point)` won't run, so user must press a key in order the code to resume running. in the `9th line (typing the line)` again program halts and waits until user hit Enter button. I think the only problem is that it won't show running timer on terminal screen while user is typing. – Shoaib Mirzaei Oct 02 '20 at 12:53
  • Yes, I see that now, thanks. Your explanation in the comments was helpful. – rajah9 Oct 02 '20 at 13:39