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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:27:26+00:00 2026-05-26T18:27:26+00:00

I have a keylistener attached to my frame in java, i can detect key

  • 0

I have a keylistener attached to my frame in java, i can detect key presses when I hit any key, a strange thing is happening however. My game is a minesweeper game, I have a restart button that basically clears the board and remines it. The weird thing is when I click the button with the mouse everything clears fine and the board is remined but the keylistener stops working. Even stranger I have a jmenuitem that basically does a automated click of the button. So its like restartbutton.doclick()

if i click the jmenuitem to restart it restarts fine clears everything and the keylistener still functions. I can even see the button being clicked. Any ideas why this could be happening?

Thanks

this is attached to my main frame. this is the listener that stops working after clicking the button.

frame.addKeyListener(new KeyListener(){


       public void keyReleased(KeyEvent e){


       }

       public void keyPressed(KeyEvent e){

       System.out.println("hey");
       int keycode = e.getKeyCode();

       if(e.isControlDown() & keycode==KeyEvent.VK_C){

      balh blah balh
       }

       }

       public void keyTyped(KeyEvent e){


       }

       });
  • 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-26T18:27:27+00:00Added an answer on May 26, 2026 at 6:27 pm

    Suggestions:

    • Yours is a focus issue, where the KeyListener stops working because the container it is listening to has lost focus to the JButton.
    • One solution is to make the JButton not able to gain focus by calling setFocusable(false) on it.
    • But I recommend that you don’t use a KeyListener at all if possible, but rather key bindings, since with bindings you don’t have this issue and also it is a higher level construct.

    Edit
    Regarding:

    what would be the best way to change that to a key binding?

    Best would be to go through the Key Bindings tutorial and to implement the recommendations found there.

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

Sidebar

Related Questions

Can you have a keylistener in a java console application without messing with JNI?
i have implemented keylistener interface and implemented all the needed methods but when i
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
I wish to add a KeyListener to the entire UI by using: frame.addKeyListener(this); In
All of the examples for key listeners I have been able to find deal
I'm writing a simple program in Java which includes a KeyListener with the following
I'm writing a Java application that will have an on-screen number pad available for
I'm working on a game in Clojure using Swing. I have a KeyListener to
I'm battling an error I don't quite understand. I have a class in Java,

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.