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

  • Home
  • SEARCH
  • 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 892031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:59:09+00:00 2026-05-15T13:59:09+00:00

Pressing space bar in game will make a character shoot, pressing space bar when

  • 0

Pressing space bar in game will make a character shoot, pressing space bar when a confirmation box is shown will make this box disappear and pressing space bar in a highscore form will add a space in an input box. In this example there are several events for the same key, but only one is fired at a time.

Is there a general (or specific for Javascript) method or way of programming to add events to a certain key, so they are only executed under certain circumstances?

Of course it can be done like this:

var inGame = true|false;
var inConfirmationBox = true|false;

function spaceBarHandler(){
    if(inGame){ /*shoot*/}
    else if(inConfirmationBox){ /*remove box*/}
}

document.onkeydown = function(){ /* call space bar handler if space bar was pressed */ };

But this is a very confusing way of programming, since specific actions are mixed together in a space bar handler function, which makes maintenance hard.

What is the best way to handle multiple events for one key, such that these events are only fired under certain circumstances?

  • 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. Editorial Team
    Editorial Team
    2026-05-15T13:59:09+00:00Added an answer on May 15, 2026 at 1:59 pm

    Functions are first-class objects in javascript, which makes them really powerful. Because of this, your problem can be solved very elegantly.

    // the whole thing can be encapsulated 
    // into an object actually
    
    function spaceBarHandler() {
      var state = spaceBarHandler.state;
      var actions = spaceBarHandler.actions;
    
        // execute function if exists
        if (actions[state]) {
          actions[state]();
        }
    }
    
    // spaceBar actions
    spaceBarHandler.actions = {
      shoot: function() {
        // bang bang
      },
      removeBox: function() {
        // do it...
      }
    };
    
    // change current state from outside
    // we are in the game
    spaceBarHandler.state = "shoot";
    
    // change current state from outside
    // confirmation box is shown 
    spaceBarHandler.state = "removeBox";
    

    All these cases will be handled by one function. If you want to extend with another case, you just add another function to the actions object. Notice how the whole thing is encapsulated into one object.

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

Sidebar

Related Questions

How can I delete a line form a bound DataGridView when pressing the Delete
Well I am trying to submit a form by pressing enter but not displaying
My application hangs. Pressing the home button doesn't return to the springboard anymore. Additionally
How to cancel a keypress event in a textbox after pressing the return key.
Under visual studio if you run a web project by pressing the Go button
In most versions of windows, you can get to the menu by pressing the
with (x)emacs, how could I run the 'compile' command without separately pressing enter to

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.