0

I want to capture the pressed key and do different tasks if it for example is '1' or '2', etc.

This is my sample code:

import keyboard

key = keyboard.read_key()
if key == '1':
    print('something')
elif key == '2':
    print('something')
else:
    print('something')
    
str = input('Type something: ')

It works fine but for example when I press key '1', when the last line (input) runs, the output is like this (Terminal):

Type something: 1

Why Python automatically puts '1' at the end and how can I get rid of it?

Appreciate any help!

Afshin Davoudy
  • 43
  • 2
  • 11

1 Answers1

0

You could try swapping keyboard.read_key() for the keyboard.is_pressed() function to process the input, depending on the keys pressed

The post below might help more:

How to detect key presses?

jrynes
  • 187
  • 1
  • 9
  • keyboard.is_pressed() waits just for a specific key, while the purpose is to execute different codes based on different inputs! – Afshin Davoudy Nov 13 '22 at 19:12