GetKeys tutorial (Python)
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.
Importing
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
Extras
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! 😀
You can add syntax highlighting to your code blocks by adding
py
after the 3 backslashes (`):Isn't it
if k == "a" or key == "A":
?Also, you can use
getch,getch()
::D
@Bookie0 For
Without adding the name of the object again it checks if the object
key
has eithera
orb
as it's value.@MuffinsTheCat oh hm