How can a KeyListener detect key combinations (e.g., ALT + 1 + 1)

前端 未结 6 1338
悲哀的现实
悲哀的现实 2020-11-27 07:02

How can I let my custom KeyListener listen for combinations of ALT (or CTRL for that matter) + more than one other key?

Assume

6条回答
  •  孤城傲影
    2020-11-27 07:46

    I think there's a simpler way which I am using. If the KeyEvent is ev then if you investigate:

    (int)ev.getKeyChar()
    

    you find that ctrl-a is 1, ctrl-b is 2 and so on. I wanted to use ctrl-s for save. So I just use:

    (((int)ev.getKeyChar())==19)
    

    to detect it. No idea why, but it works fine whereas:

    ev.isControlDown() && ev.getKeyChar()=='s'
    

    does not.

提交回复
热议问题