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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:03:37+00:00 2026-06-07T00:03:37+00:00

First of all, this isn’t for a keylogger, it’s for an input in a

  • 0

First of all, this isn’t for a keylogger, it’s for an input in a roguelike game where the JLabel in my JFrame will say “Name: ” and I want to be able to type A-Za-z. After having a look at lots of options for key input, I am back where I started using KeyStrokes and Actions.

What I am wondering is whether there is a way to add a range of keys, rather than repeating this 56 times:

Action a = new AbstractAction() {

    public void actionPerformed(ActionEvent e) {
        // add a to string
    }
};
getInputMap().put(KeyStroke.getKeyStroke("a"), "a");
getActionMap().put("a", a);

I mean, I could do it this way, just write a bit of copy and pasting, but I really do hate that. There has to be a more elegant solution than using KeyListener which isn’t very good as it means I have to somehow have focus but my window is simply one JFrame with a big JLabel in it.

Also, I’ve only been doing java a few days now, so that’s why I am probably missing the a very commonly known solution, but if there is one, please do share! Appreciated.

  • 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-07T00:03:38+00:00Added an answer on June 7, 2026 at 12:03 am

    I am using a content manager class which treats each “screen” as a JPanel which is set as the content pane, I put a reference to the parent JFrame into the base “screen” class and then added a KeyListener to the parent JFrame. This captures input just how I want it to. I didn’t do this before as I mistakenly added the KeyListener to the content pane. It all appears to work fine now.

    public void run() {
        this.getParent().addKeyListener(new KeyListener() {
    
            public void keyPressed(KeyEvent e) {
                appendLabel(e.getKeyChar());
            }
    
            public void keyReleased(KeyEvent e) { }
            public void keyTyped(KeyEvent e) { }
        });
    }
    

    Thanks @millimoose!

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

Sidebar

Related Questions

First of all, sorry if this isn't an appropriate question for StackOverflow. I've tried
First of all, this isn't another question about storing images on DB vs file
first of all sorry if this isn't formatted correctly, first time doing this. I've
Good morning, all. This is my first question on stackoverflow, so hopefully this isn't
First of all this is my markup: <div class=first></div> <div class=second></div> <div class=second></div> <div
first of all: this is not the same as this . The ModelBackend has
first of all this is my third question about web services here and i
First of all: This question is not directly programming related. However, the problem only
Well, first of all sorry about this question it must be pretty straight forward
Maybe it's a similar beginning, but it's true. first of all sorry if this

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.