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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:48:21+00:00 2026-06-18T04:48:21+00:00

I need to take in customizable actions read from a file and bind them

  • 0

I need to take in customizable actions read from a file and bind them to a key for a program.

If I have a file like so:

w:up
s:down
a:left
d:right

How would I go about getting this running?

The only way I see it is doing this:

// hardcoded String to Action to turn "up" into an actual instruction
HashMap<String, Action> actions = new HashMap<String, Action();
actions.put("up", new Up());
actions.put("down", new Down()); // etc.


HashMap<Integer, Action> keybindings = new HashMap<Integer, Action>();
while (!endOfFile) {
    int key = letterToKeycode(getKey()); // gets keycode for letter
    Action action = actions.get(getCommand());
    keybindings.put(key, action);
    endOfFile = isEndOfFile();
}

Then when my keylistener method gets called, it does:

public void keyPressed(int keycode) {
    keybindings.get(keycode).doAction();
}

and doAction() would be in each Action class. So if I had Up(), it would call person.moveUp().

If I was to rebind most of my keys, it could result in hundreds of classes that are a few lines long.

There’s something about the above concept, and a single switch statement that makes me want to avoid them. Is there “cleaner” trick to doing this?

As an example of what I mean, Eclipse has keybindings you can set in the preferences, so when you hit a key, it fires off an event and interprets what the key is supposed to do based on those settings. I’m attempting to do this.

  • 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-18T04:48:23+00:00Added an answer on June 18, 2026 at 4:48 am

    Rather than create your own Map, use Key Bindings; LinePanel is an example. You may be able to parameterize or re-factor your Action implementation(s) to mitigate proliferation. To persist user-defined key assignments, you may be able to adapt the approach used in the example cited here.

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

Sidebar

Related Questions

I need to take rows from two separate tables, and arrange them in descending
I need to take data from external file and format it, the problem is,
I need to take all plus symbols from a variable and replace them with
I have a GridView. At event _RowDataBound I need take the underlying value from
I need to take an input (date) from a form in a php file,
I need to take a .csv file from an HTML form and parse through
I need to take a string that was originally read in from a text
I need to take an input from a WYSIWYG and break it down to
Need to take a SELECT drop down list options and find if any of
I need to take a large file, with lines such as: member: cn=user0001,ou=people And

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.