Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 94081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:26:44+00:00 2026-05-10T23:26:44+00:00

What is a good way to implement keyboard handling? In any language, where I

  • 0

What is a good way to implement keyboard handling? In any language, where I write a keyboard-interactive program (such as a tetris game), I end up having some code that looks like this:

for event in pygame.event.get():     if event.type == KEYDOWN:         if False: pass          #make everything an elif         elif rotating: pass         elif event.key == K_q:         elif event.key == K_e:         elif event.key == K_LEFT:             curpiece.shift(-1, 0)             shadowpiece = curpiece.clone(); setupshadow(shadowpiece)         elif event.key == K_RIGHT:             curpiece.shift(1, 0)             shadowpiece = curpiece.clone(); setupshadow(shadowpiece) 

(shortened). I don’t like this, as this has to go in my main loop, and it messes with all parts of the program. This also makes it impossible to have a user config screen where they can change which key maps to which action. Is there a good pattern to do this using some form of function callbacks?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:26:44+00:00Added an answer on May 10, 2026 at 11:26 pm

    You could create a dictionary where the keys are the input and the value is a function that handles the keypress:

    def handle_quit():   quit()  def handle_left():     curpiece.shift(-1, 0)     shadowpiece = curpiece.clone(); setupshadow(shadowpiece)  def handle_right():     curpiece.shift(1, 0)     shadowpiece = curpiece.clone(); setupshadow(shadowpiece)  def handle_pause():     if not paused:         paused = True  branch = {   K_q: handle_quit   K_e: handle_pause   K_LEFT: handle_left   K_RIGHT: handle_right }  for event in pygame.event.get():     if event.type == KEYDOWN:         branch[event.key]() 

    Then changing the keys is a matter of modifying keys of the dictionary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a game and I'm looking for a good way to implement multiplayer.
I'm looking for a good way to implement a truly modal dialog in Silverlight
TL;DR: What is a good and testable way to implement the dependency between the
What's a good way to implement api keys for accessing specific controller actions using
What's a good way to implement mutable data structures in F#? The reason I’m
What's a correct and good way to implement __hash__() ? I am talking about
I've been searching around for a good way to implement the hierarchy below. I've
Can anyone tell me what would be a good way to implement soft deletes?
Warm-up I'm trying to come up with a good way to implement customized document
Is there a good way to implement many-to-many relation between rows in single table?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.