Skip to content
← Back to Community
@Reuben123's Python Keypress Detection - The Basic Tutorial
Profile icon
Reuben123

Hello fellow programmers,

Here is my tutorial for key detection in python!
Using readchar, we can detect and interpret key presses in python, without having to press enter after key presses.

I have created a demo / example project demonstrating this. Below is also a step by step guide for how you can do this too.

Here's how:

  1. Importing readchar
    from readchar import readchar

  1. Creating your variable
    your_variable_here = readchar()

  1. Input and keypress functions
if your_variable_here == '1': #1 - First Keypress print("Hello, World!") elif your_variable_here == '2': #2 - Second Keypress print("You're Awesome!") else: #If input != '1' OR '2' print("Invalid Input")

That's it.

These are the basics, you can add more functions and change what you want. This was just meant to be a simple guide to get you started.

If you would like to see a slightly more complex example of this see @52eagle's post:

https://repl.it/talk/share/Arrow-keys-and-such-using-from-readchar-import-readkey/28853?order=votes

If you need further help or have any suggestions, feel free to put them in the comments. I'll be giving everyone cycles :)

I'm quite new to python too so feel free to give me any tips or advice that could help me improve.

All the best and hope this helps 👍

by @Reuben123

Voters
Profile icon
wolfieboy09
Profile icon
EpicJohn127
Profile icon
MyloBishop
Profile icon
SamFleming
Profile icon
ParkerHance
Profile icon
Reuben123
Profile icon
DominicHuieWhit
Profile icon
Hex-
Profile icon
adl212
Comments
hotnewtop
Profile icon
aprillsavells

is it posible to detect clicks?

???

Profile icon
adl212

@Reuben123 this is a great tutorial and is really helpful. Thanks!

Profile icon
Reuben123

@adl212 No problem :)

Profile icon
coderash

omg thx, i need this for a project im working on :D

Profile icon
Reuben123

@coderash Good timing then! Glad I could help :D Gave you a cycle.

Profile icon
coderash
Profile icon
Reuben123

@coderash No problem. Hope all goes well with your project. If you need some help, even though I'm not the best, I could help you out or create another tutorial :)

Profile icon
coderash

Haha yeah, know anything about django xD im building a form-making [email protected]

Profile icon
Reuben123

@coderash Sound great :) I would have to do some learning lol

Profile icon
coderash

:)

Profile icon
aprillsavells

@Reuben123 is it possible to detect clicks ??

Profile icon
wolfieboy09

is there a way to detect arrow keys?

Profile icon
GenGrief

This is a great tutorial that really helped me! I'm working on a project and this is perfect for it! Thanks so much!!
One question though - how would you make it loop a main body of code?

Profile icon
themajikspoon

This is really useful, Thanks!!!

Profile icon
Legendthecoder

@Reuben123 Hey it works very good!, Can you please explain to me how it works?

Profile icon
SomeOSCMember

@Legendthecoder Well I'm not @Reuben123, but the readchar function works by reading the first character pressed on the keyboard after doing whatever you told the program to do (In this case, print instructions), and you can store this into a variable to be used for other things later (like if the user pressed '1' you could print 'hi'). It's also pretty nice because you don't have to hit enter to get the program to process the input.

Profile icon
INCOGNITO001

@Reuben123 Other people seem not to be having trouble with this tutorial, but for me it says there is no module called readchar.
Do you have any idea why this might be? I ran your example program and it gave me exactly the same error.

Profile icon
SomeOSCMember

@INCOGNITO001 To fix this, you can switch to the 'shell' tab and type in 'pip3 install readchar' or you can go to packages and search up 'readchar' and press the little plus sign to download it.

Profile icon
LucasLum

Just wondering, is there a way to detect this in Python Turtle?

Profile icon
wolfieboy09

@LucasLum I don't think so

Profile icon
DominicHuieWhit

as a n00b this is amazing