Skip to content
← Back to Community
GetKeys tutorial (Python)
Profile icon

How to use GetKeys

What is GetKeys?

GetKeys is a Python module that allows you to detect a user input without them having to press enter afterwards.


Write from getkeys import getkeys, keys to import getkeys and an extra feature

Key detection

To detect a key being pushed, write k = getkey(). Note that the program will not continue until a key is pressed

Using k

Basic characters

For characters such as letters, numbers, symbols and spaces, write this after k = getkey():

if k == "a": #change the a to whatever you want #Stuff goes here else: #Stuff goes here

Or to make it non-case-sensitive:

if k == "a" or "A": #Stuff goes here else: #Stuff goes here

Other characters

For escape, backspace, delete, enter, tab, home, end, insert and the arrow keys it is a little bit different.

if k == keys.ENTER: #Change the ENTER to one of the keys above. It must be capitalized and not abbreviated (eg. ESC and DEL should be ESCAPE and DELETE #Stuff goes here else: #Stuff goes here


You can also use elif to detect which key is pressed. For example:

if k == "a": #Stuff goes here elif k == "b": #Stuff goes here else: print("Must press [a] or [b]!")

Thank you for taking this tutorial. Have a great day! 😀

Profile icon
Profile icon
Profile icon
Profile icon
Profile icon

Screen Shot 2021-03-17 at 3.21.42 PM

You can add syntax highlighting to your code blocks by adding py after the 3 backslashes (`):

if k == "a": #change the a to whatever you want #Stuff goes here else: #Stuff goes here

if k == "a" or "A":

Isn't it if k == "a" or key == "A":?

Also, you can use getch,getch():

from getch import getch char = getch.getch() if char.lower() == "a": # for letters print("You pressed the \'a\' key") elif char == "1": # numbers print("You pressed the \'1\' key") elif char == ",": # symbols print("You pressed the \',\' key") # you can use ord to check the ascii values of keys like enter, tab, backspace, etc. elif ord(char) == 9: # tab print("You pressed the \'tab\' key")


Profile icon

@Bookie0 For

if k == "a" or "b":

Without adding the name of the object again it checks if the object key has either a or b as it's value.

Profile icon