Learn to Code via Tutorials on Repl.it!

← Back to all posts
@Reuben123's Python Keypress Detection - The Basic Tutorial
Reuben123 (26)

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

2) Creating your variable
your_variable_here = readchar()

3) Input and keypress functions

if your_variable_here == '1':
   #1 - First Keypress
   print("Hello, World!")
elif your_variable_here == '2':
   #2 - Second Keypress 
   print("Your Awesome!")
   #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:


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

aprillsavells (1)

is it posible to detect clicks?


adl212 (153)

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

coderash (285)

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

Reuben123 (26)

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

Reuben123 (26)

@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 :)

coderash (285)

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

Reuben123 (26)

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

aprillsavells (1)

@Reuben123 is it possible to detect clicks ??

gjirtngisnt (12)

@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.

LucasLum (4)

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

DominicHuieWhit (46)

as a n00b this is amazing