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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:26:30+00:00 2026-05-30T05:26:30+00:00

I have a class that manages keyboard input and fires off KeyPressed , KeyReleased

  • 0

I have a class that manages keyboard input and fires off KeyPressed, KeyReleased, or KeyHeld events. It only fires off an event if the key exists in the KeyBindings collection of my Controller component. Now that I’ve got all of that working I’m stuck on a problem. What I want is the following:

Key pressed.

if(Key bind exists)
    Fire key pressed event.

foreach(function in keyBinds)
{
    execute function, fire event, whatever...
}

I just can’t figure out how the foreach loop would work. Any ideas on how I could pull something like this off?


KeyboardController Component:

public class KeyboardController : IComponent
{
    //Fields
    private Dictionary<Keys, HashSet<Delegate>> m_keyBindings = new Dictionary<Keys,HashSet<Delegate>>();

    //Properties
    public Dictionary<Keys, HashSet<Delegate>> KeyBindings
    {
        get { return m_keyBindings; }
    }
}

This is the class that will contain the Keys and their function/delegate/event/whatever bindings. The code for events CANNOT be contained within this class because the class is meant only to store data. I need to pass a Key bind and an action or set of actions to perform when this bind is pressed.

Adding a bind:

//Set key bindings
KeyboardController kbController = entityManager.GetComponent<KeyboardController>(1);
kbController.KeyBindings.Add(Keys.Up, new HashSet<Delegate>());
kbController.KeyBindings[Keys.Up].Add(function);

I don’t know how to make the third line in “Adding a bind:” work.

  • 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-30T05:26:31+00:00Added an answer on May 30, 2026 at 5:26 am

    You can use a multicast delegate to automatically fire off multiple events for a given key, this way you don’t need to maintain a collection of events. For example:

    Dictionary<Key, Action> bindings = ...
    Action binding;
    if (binding.TryGetValue(key, out binding))
      binding(); // this will execute multiple events if they are hooked
    

    Hooking events:

    bindings[Keys.A] += new Action(MyAKeyHandler);
    

    If for some reason you didn’t want to use multicast delegates, you could do something like this:

    List<Action> handlers = binding[key];
    ...
    if (handlers != null)
      foreach (var handler in handlers)
        handler();
    

    Rather than HashSet use an actual delegate type such as Action<>. For instance:

    Dictionary<Keys, Action> handlers = ...
    handlers[key] += function;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some event handler on a boundary class that manages a persistence mechanism
I have a GUI tool that manages state sequences. One component is a class
Suppose we have a class called AccountService that manages the state of accounts. AccountService
I have an existing application in C++ with a custom ArrayBase class that manages
I have a class that manages user preferences for a large software project. Any
I have a class that manages the creation of RTF documents and a method
I have a system that manages events that happen to multiple actors using a
I have a Visual Studio 2008 C++ project with a class that manages a
I have a Singleton class that manages the connection to an external device. The
I have a special class that manages gestures and other things. It is strongly

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.