7

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){


       }

       });
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user541597
  • 4,247
  • 11
  • 59
  • 87

4 Answers4

14

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • attached is my key listener code what would be the best way to change that to a key binding? – user541597 Nov 10 '11 at 02:39
  • Even if you use keybindings, you still have to make the jbuttons not focusable, even when using `somerootcomponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)` – Dylanthepiguy Aug 28 '17 at 22:19
2

this is focus problem, you can use this code to give focus again

getTopLevelAncestor().requestFocus();

Emre Yardımcı
  • 324
  • 1
  • 5
  • 11
1

Based on the answer to this similar question, I implemented the KeyEventDispatcher rather than using the default listeners. I believe this function will be fairly global though, so you might need to check and make sure the right things are visible/focused.

    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher( new KeyEventDispatcher() {
      public boolean dispatchKeyEvent(KeyEvent e) {
          if (e.getID() == KeyEvent.KEY_PRESSED) {
              System.out.println("tester");
          }
          return false;
      }
Community
  • 1
  • 1
Alan Jackson
  • 6,361
  • 2
  • 31
  • 32
0

I was able to solve this problem by setting the setFocused property of the container to be true:

frame.setFocusable(true);
Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
Meshu
  • 35
  • 1
  • 5