Skip to content
Sign upLog in
← Back to Community
I need to do the keyboard detect
Profile icon
TheAlphaReturns

first off, I use "</>" to detect code
I always use the code in python:
</>
if keyboard.is_pressed(this key)
"do this"
<>
it simply does not work on my repl.
I'm trying to do this:
</>
print("1. Play a new game")
print("2. Play a 1-Player version(using random variable)")
print("3. How to Play")
print("Type Option:")
if keyboard.is_pressed("1")
from twoplayer import play
<>
the "twoplayer.py" is a file that I have already made with a function called play. If it works it should print hello
this is the code in twoplayer.py
</>
def play():
print("hello")
<>
I am getting this error about invalid syntax on the if keyboard.is_pressed(1) line!!! This worked perfectly on IDLE!!!
Does anybody have an alternative?

Answered by TheAlphaReturns [earned 5 cycles]
View Answer
Voters
Profile icon
TheAlphaReturns
Comments
hotnewtop
Profile icon
Vandesm14

See if this works for you: https://pypi.org/project/keyboard/

Profile icon
TheAlphaReturns

So, I can't find this code in my Repl's, but I did some research and figured that the function :

input()

worked
all I needed to do was:

keyboard_press = input() #create a variable if keyboard_press == '1': do this elif keyboard_press == '2': do this other thing

this worked perfectly for anyone who needs an answer

Profile icon
TheAlphaReturns

I ALREADY tried that! The one that you suggested, the keyboard.

Profile icon
MarcBauchet

@Vandesm14
as the link you sent says: "Works with Windows and Linux (requires sudo)". Hence in Replit,
import keyboard --> "ImportError: You must be root to use this library on linux."

Profile icon
MarcBauchet

with that one, no need for root access in linux:
https://sshkeyboard.readthedocs.io/en/latest/
i had to install it in Replit though