How can i use getch kbhit in Python

2.1k Views Asked by At

How can i use kbhit function in getch, when I am using getch library in Python and using Linux? I found kbhit function in msvcrt library, but I do not use msvcrt, because it is only for Windows and i cannot found anything for getch library.

This is my code right now:

import getch
import threading

def onkey():
    while getch.kbhit():
        input = str(getch.getch())
        print(input)

t = threading.Thread(target=onkey)
t.daemon = True
t.start()
1

There are 1 best solutions below

0
Deadjim On

KBHIT - "Returns True if keyboard character was hit, False otherwise."

So when you press any key it will return True. If you want to know which key you've pressed:

import getch

key = getch.getch().decode('utf-8')

print(key)