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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:37:36+00:00 2026-06-15T00:37:36+00:00

I am pretty sure I am missing something pretty simple here, but I just

  • 0

I am pretty sure I am missing something pretty simple here, but I just can’t find it!

Here is the declaration for my GamePanel class:

private class GamePanel extends JPanel implements KeyListener {

In its constructor is:

addKeyListener(this);

Also in the constructor:

        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    index ++;
                    index = index >= 15 ? 0 : index;
                    if (aPress) {
                        playerX --;
                    }
                    if (dPress) {
                        playerX ++;
                    }
                    repaint();
                    try {
                        Thread.sleep(25);
                    } catch (InterruptedException e) {}
                }
            }

        }).start();

In the class:

    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println("Key pressed!");
        if (e.getKeyCode() == KeyEvent.VK_A) {
            aPress = true;
        }
        if (e.getKeyCode() == KeyEvent.VK_D) {
            dPress = true;
        }
    }
    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_A) {
            aPress = false;
        }
        if (e.getKeyCode() == KeyEvent.VK_D) {
            dPress = false;
        }
    }

And finally:

JFrame f = new JFrame();
//set up f
f.add(new GamePanel());
f.setVisible(true);

I even added a System.out.println in my keyPressed method and it won’t print! What am I missing?

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

    KeyListener is a fickle mistress. The component where the KeyListener is registered must be focusable and must have focus in order for it to receive key events.

    Try adding setFocusable(true); and requestFocusInWindow(); after you have registered the listener.

    A better idea would be to use key bindings, this will over come the focus issue.

    InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
    ActionMap am = getActionMap();
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), "a.pressed");
    am.put("a.pressed", new AbstractAction() {
        public void actionPerformed(ActionEvent evt) {
            aPress = true;
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm probably missing something simple here, but I can't find the answer elsewhere. I
Okay, I'm pretty sure I'm missing something very obvious here, but I just couldn't
Pretty sure there is an easy answer to this, but just can't find the
I'm pretty sure this is really simple, and I'm just missing something obvious. I
Pretty sure that I'm missing something really simple here: I'm trying to display a
I'm sure I'm missing something pretty basic, but I have just started to get
I'm pretty sure I'm missing something simple, but since I've been breaking my head
I have a pretty simple scenario and I'm sure I'm just missing something obvious.
I'm sure I'm missing something pretty simple here. I've created a custom DateTime display
I'm pretty sure I'm missing something very elementary here as I'm normally ok with

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.