Setting button mnemonic key events without having to use ALT

时间秒杀一切 提交于 2019-12-10 18:49:50

问题


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.


回答1:


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




回答2:


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());



来源:https://stackoverflow.com/questions/8775364/setting-button-mnemonic-key-events-without-having-to-use-alt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!