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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:29:33+00:00 2026-06-09T15:29:33+00:00

Okay, so I’m pretty frustrated here. This piece of code here is just a

  • 0

Okay, so I’m pretty frustrated here.
This piece of code here is just a very simple moving JComponent.

What’s odd is how, when I’m changing absolutely nothing, the keyPressed event will be incredibly inconsistent. I start the program, and sometimes it’ll work, and my ball will move around the screen. On the other hand, I’ll close it and open it without changing anything, and it’ll fail to work. I don’t think focus is a problem here, though I really don’t know much about it. I have no idea what’s going on.

Any help would be appreciated. I just don’t see how the program could so inconsistently fail and succeed.

And here’s my code in the character’s class, because I don’t think just giving you a snippet will help. I don’t know if it’s just me, or whatever, but if you want to compile it and see, go ahead.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Character extends JComponent implements KeyListener
{    
Timer timer = new Timer(5, new TimeListener());
private int x = 250;
private int y = 300;
char whichTimer;


public Character()
{
    addKeyListener(this);
    setFocusable(true);
    requestFocusInWindow();
    repaint();
}

public void keyReleased(KeyEvent e)
{
    if(e.getKeyCode() == KeyEvent.VK_W)
    {
        timer.stop();
    }
    if(e.getKeyCode() == KeyEvent.VK_A)
    {
        timer.stop();
    }
    if(e.getKeyCode() == KeyEvent.VK_S)
    {
        timer.stop();
    }
    if(e.getKeyCode() == KeyEvent.VK_D)
    {
        timer.stop();
    }
}
public void keyPressed(KeyEvent e)
{

    if(e.getKeyCode() == KeyEvent.VK_W)
    {
        timer.stop();
        whichTimer = 'W';
        timer.start();
    }

    if(e.getKeyCode() == KeyEvent.VK_A)
    {
        timer.stop();
        whichTimer = 'A';
        timer.start();
    }
    if(e.getKeyCode() == KeyEvent.VK_S)
    {
        timer.stop();
        whichTimer = 'S';
        timer.start();
    }
    if(e.getKeyCode() == KeyEvent.VK_D)
    {
        timer.stop();
        whichTimer = 'D';
        timer.start();
    }
}
public void keyTyped(KeyEvent e)
{

}
protected void paintComponent(Graphics g)
{
    super.paintComponent(g);
     Graphics2D g2d = (Graphics2D) g;

       // g2d.drawImage(fatsprite, x-10, y-10, null);
    g.setColor(Color.BLACK);
    g.fillOval(x-10, y-10, 20, 20);

}

class TimeListener implements ActionListener
{
    public void actionPerformed(ActionEvent e) 
    {
        if(whichTimer == 'W')
        {
            y-=1;
            repaint();
        }
        if(whichTimer == 'A')
        {
            x-=1;
            repaint();
        }
        if(whichTimer == 'S')
        {
            y+=1;
            repaint();
        }
        if(whichTimer == 'D')
        {
            x+=1;
            repaint();
        }
    }

}
}
  • 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-09T15:29:34+00:00Added an answer on June 9, 2026 at 3:29 pm

    @Max is correct, KeyBindings is better approach to your problem. The key problem (sorry, no pun intended) is that once focus moves to any other component, you will no longer receive key events. Now, you’re probably thinking, “but I didn’t change focus” – you only need to click on another focusable component to loss key focus, or press tab, which KeyListener won’t generally be notified of.

    Generally, as you’ve found, KeyListeners are unreliable, not only are they only ever going to get notification while the component in question has focus, keys can be consumed before they reach you

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

Sidebar

Related Questions

Okay this question is very simple: I have a facebook page, and a website.
Okay, this is probably a very basic question; but, I'm just getting back in
Okay so my question is this. Say I have a simple C++ code: #include
Okay, so this is a long long shot but here it goes... When IE
Okay, this one is pretty obvious to everyone who use Django and frequently asked
OKay, this is probably pretty noob, but I couldn't find how to solve it.
Okay. I know this looks like the typical Why didn't he just Google it
Okay so I know the title is very vague but here is my more
Okay so I do this (not actual code) try { final View view =
Okay, I feel a bit foolish for having to ask this but I guess

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.