4

Is there a way to set a button key event in Java so that Alt does not have to be pressed. For example, when this is used setMnemonic(KeyEvent.VK_DELETE) it is required for Alt + Delete to be pressed in the application. How can I get around this? Thanks.

mKorbel
  • 109,107
  • 18
  • 130
  • 305
John
  • 1,546
  • 6
  • 17
  • 28

2 Answers2

11

I would take a look at the key bindings tutorial. You can specify any KeyStroke to perform any Action.

Jeffrey
  • 43,506
  • 7
  • 88
  • 140
-1

Create a KeyListener, or extend a KeyAdapter. Like this:

private class MnemonicWorkaround extends KeyAdapter{

     @Override
     public void keyPressed(KeyEvent e) {
        int c = e.getKeyCode();
        if(c == KeyEvent.VK_ENTER){
         // do something.
        }
      }
  }

Then add it using component.addKeyListener(new MnemonicWorkaround());

rtheunissen
  • 7,079
  • 5
  • 31
  • 63