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 8379251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:07:40+00:00 2026-06-09T16:07:40+00:00

In my C#/XNA project, I have a static class that manages the input. It

  • 0

In my C#/XNA project, I have a “static” class that manages the input. It looks like this:

internal sealed class InputManager
{
    public delegate void KeyboardHandler(Actions action);

    public static event KeyboardHandler KeyPressed;

    private static readonly Dictionary<Actions, Keys> KeyBindings = Main.ContentManager.Load<Dictionary<Actions, Keys>>("KeyBindings");

    private static KeyboardState currentKeyboardState;

    private InputManager()
    {
    }

    public static void GetInput()
    {
        currentKeyboardState = Keyboard.GetState();

        foreach (KeyValuePair<Actions, Keys> actionKeyPair in KeyBindings)
        {
            if (currentKeyboardState.IsKeyDown(actionKeyPair.Value))
            {
                OnKeyPressed(actionKeyPair.Key);
            }
        }
    }

    private static void OnKeyPressed(Actions action)
    {
        if (KeyPressed != null)
        {
            KeyPressed(action);
        }
    }
}

So throughout the game, I get the input and check if any key contained in my dictionary is currently pressed (I use a dictionary for key binding purpose – an action is bound to a key). If so, the KeyPressed event is fired with the associated action as argument. By doing this I can subscribe an external class (such as a camera) to this event and do the appropriate thing based on the action (key).

The problem is that I have to test the action in every subscriber’s method like this:

        if (action == Actions.MoveLeft)
        {
            DoSomething();
        }

Hence, no matter which key is pressed (as long as it’s part of the dictionary) every subscriber’s method will be called even if it actually doesn’t need to.

I know I could set an event for each action : event MoveLeft, event MoveRight, etc… However, is there a better way to do this like a list of event?

  • 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-06-09T16:07:42+00:00Added an answer on June 9, 2026 at 4:07 pm

    you can use interface to deal with:
    for instance:

    interface IExecuteAction
    {
    void doMoveLeftAction();
    void doMoveRightAction(); 
    }
    
    ExecuteMoveLeftAction(Actions action,IExecuteAction execAction)
    {
    switch(action
     case Actions.MoveLeft :
            execAction.doMoveLeftAction(); 
            break; 
    case Actions.MoveLeft :
            execAction.doMoveLeftAction(); 
            break;
    }
    

    in your code implement this interface on your subscriber

    class subscriber1:IExecuteAction
    {
    void doMoveLeftAction()
    {
    //do what you want
    }
    void doMoveRightAction()
     {
     //do what you want
     }
    }
    

    and after you handle like this:

    ExecuteMoveLeftAction(action,subscriber1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XNA 4.0 project that I would like to be compiled with
I have just started learning XNA. This is my first program that I am
I have an XNA 3.1 program and would like to create an installer that
I have an XNA project on WP7. I would like to get access to
I'm working on a C# XNA project that requires me to display information based
When running this XNA application it should display a rotated rectangle that moves from
I have a standard 800x600 window in my XNA project. My goal is to
I have been using Ninject as the IOC for an XNA project, and was
I have a WinForms application that uses XNA to animate 3D models in a
Hey all, i've been working on this small project in XNA the goal is

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.