0

I'm having trouble using the keyboard module. I'm working on a typing game in turtle graphics. The user types and a "guide" arrow above the sentence shows the user how far they've typed. The arrow is supposed to turn red when an incorrect letter is typed, and it turns green when a correct letter is typed.

Knowing if the user has typed the correct letter is no problem, I'm using keyboard.is_pressed() to move the arrow forward and change green. However, the incorrect part is the problem. I need to use a function that returns the value of a any key, not a specific key. If it returns the value of the key the user types, then I can see if it is incorrect or not.

I tried using the conditional: if keyboard.read_key() != letter: which does what I want, but since I am using keyboard.is_pressed() to see if the letter is correct or not, the arrow only changes green for an instant, and then goes red. This is the code I am using:

    count1 = 0
    while True:
        if x[count1].isupper():        
            if keyboard.is_pressed(x[count1]) and keyboard.is_pressed('shift'):
                carmove()
                arrowmove()
                count1 += 1
            try:
                if keyboard.read_key() != x[count1]:
                     incorrect()
            except:
                IndexError
        else: 
            if keyboard.is_pressed(x[count1]):
                carmove()
                arrowmove()
                count1 += 1
            try:
                if keyboard.read_key() != x[count1]:
                     incorrect()
            except:
                IndexError
        if count1 == length1:
            break

x[count1] is a specific letter in the sentence. This code causes the arrow to turn green for second, then it goes right back to red.

I also tried making a list of all of the printable characters, then remove the correct letter the user must type, and then iterate through the list but that didn't seem to work either.

I read through the API docs for the keyboard module, but I couldn't find anything else that might work. I'm wondering if it is possible to use the keyboard module, or if I have to use another module. Any help is appreciated.

SWEEPC
  • 3
  • 3
  • 1
    The obvious solution would be to use a single call to `keyboard.read_key()` to detect both correct and incorrect letters. – jasonharper Jan 20 '22 at 21:00
  • @jasonharper I initially commented but I didn't understand what you meant. Now I think I do. Now suppose I have to type a capital letter where I must hold down shift and press another letter. I'm not sure how I would be able to do it with `keyboard.read_key()` – SWEEPC Jan 20 '22 at 21:28

2 Answers2

0

is_pressed() function is not blocking the code to get a button click. and you also use the read_key function in the same code which is blocking the code until it gets a key.

since there is nothing here mention that the initial state for the arrow to be red. I'm assuming that the error is not in this code. this code has an error which is if you clicked the right key too fast. might not count. because is_pressed needs to be clicking the key while it executes. and another function will be blocking the code in other parts

So, I suggest this edit for your code

count1 = 0
while True:
    key = keyboard.read_key()      
    if key == x[count1]:
            carmove()
            arrowmove()
            count1 += 1
    else: 
            incorrect()
    if count1 == length1:
        break

and the read_key result for a capital key the letter in capital. no need to check for shift

  • For some reason the arrow will not move forward if the spacebar is pressed. I'm not sure why. The colour of the arrow also seems to be changing straight back to red for some reason. – SWEEPC Jan 20 '22 at 23:22
  • try to print the read_key result when you press space. and make sure that this result is in your x list. the red arrow is not in this code. it's someware in your drowning code – Mohaned AbdElMonsef Jan 21 '22 at 07:50
0

You might be able to toss the keyboard module and use turtle's own onkey() event handling. If you leave off the (second) key argument to the onkey() function, it will call your key handler code when any key is pressed. The problem is, there's no mechanism to let you know which key was pressed.

The following answer includes code to work around this design error, rewriting the underlying code to pass tkinter's event.char to turtle's event handler in the case where no key has been set (i.e. key is None):

How can I log key presses using turtle?

cdlane
  • 40,441
  • 5
  • 32
  • 81