Python Forum
Checking any key - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: Checking any key (/thread-28686.html)



Checking any key - wallgraffiti - Jul-30-2020

So I'm making a game with my own animation system, and i'm using the keyboard module for key presses. I need to check if any key is pressed. No key in specific, like, I need to check if a key is being pressed. It's hard to explain. Basically, I'm wanting to make it play an animation if no keys are being pressed, and if any key is being pressed, it will check which one it is. Is there a function for this?


RE: Checking any key - Larz60+ - Jul-31-2020

you need a keyboard listener.
You can build your own with ctypes, or use one of the available packages: https://pypi.org/search/?q=keyboard
If you choose keyboard package: https://pypi.org/project/keyboard/
see examples on README
pygame has built in event handler for this (I'm only slightly familiar with pygame as 99% of my code is not game related),
but see: https://code-knowledge.com/create-game-with-pygame-keyboard-input/


RE: Checking any key - michael1789 - Aug-01-2020

 

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        run_your_animation() ## whatever you want the key to trigger
        



RE: Checking any key - wallgraffiti - Aug-02-2020

(Aug-01-2020, 09:14 AM)michael1789 Wrote:
 

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        run_your_animation() ## whatever you want the key to trigger
        

I'm not using pygame. I'm using TKinter Canvas and the keyboard module.