When I press on "p" it triggers input with "p" in the start :
import keyboard
def print_your_name():
x = input("Enter Your Name: ")
print(x)
keyboard.add_hotkey('p', print_your_name)
keyboard.wait("esc")
output:
Enter Your Name:p
Is there any way to send a clean input to the user?
The solution that worked for me was to use the built-in msvcrt
library (on Windows). If you use a Unix-like system there is a library called termios
that I believe will do the same thing although I have not tested it. Just replace msvcrt
wherever it appears in the code with termios
.
Here is the working code
import msvcrt
def print_your_name():
x = input("Enter Your Name: ")
print(x)
def listen_for_p():
while True:
#Wait for keypress
if msvcrt.kbhit():
key = msvcrt.getch().decode()
if key == 'p':
print_your_name()
# Start listening for 'p' key
listen_for_p()
This approach directly captures keypresses from the console and doesn't rely on hooking into system-wide keyboard events, which interferes with the input()
function.