how to detect keys
I'm making a project where it gives you a letter and you have to press it in order to get a point. does anyone know how to detect which key is being pressed? by the way I am new to coding and not that good with python.
You could do like a
while true loop, then a list with all the different chars, then use random to output one of the letters, input the user for them to type a letter, and using conditionals, determine if what the user typed is that char. And a variable for the points that increments when you get it correct.
import random # imports random points = 0 # initial number of points chars = ['a', 'b', 'c'] # list containing some chars (you can modify it) while True: # while true loop random_char = random.choice(chars) # randomly selects an item out of chars print("Press " + str(random_char) + "!") # tells user what to press user_press = input("> ") # input for user to type that char # conditionals if user_press == random_char: # if user pressed that random char print("Correct!") points += 1 # adds 1 to points else: # if user didn't print("No!")
That's not really exactly what key out of the keyboard is pressed, but close enough. you can also use
getch() so you don't have to press enter (unlike
getch(), check this out.
And check out this for detecting a key press in python by installing
I recommend Pynput. Here's a tutorial of a simple keylogger: https://www.bing.com/videos/search?q=key+logger+python+tech+with+tim&&view=detail&mid=C45BAA84C64ED9013203C45BAA84C64ED9013203&&FORM=VRDGAR