【问题标题】:Know if pressing a hotkey or shortcut key in Java知道是否在 Java 中按下热键或快捷键
【发布时间】:2019-03-10 12:18:09
【问题描述】:

当快捷键 Ctrl + ACtrl + C 时如何检测Ctrl + V 被按下并继续他们的快捷键功能。他们要求我在java的这个功能下做,编辑可能只听说快捷键。这可行吗?

TCombo combo = new TCombo();
JTextComponent editor = (JTextComponent) combo.getEditor().getEditorComponent();
editor.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
    String keyChar = String.valueOf(e.getKeyChar());
    int keyCode = e.getKeyCode();

    //If Ctrl + A
    //do the normal function of select all

    //If Ctrl + C
    //do the normal function of copy

    //If Ctrl + V
    //do the normal function of paste
    }
});

【问题讨论】:

    标签: java keyboard-shortcuts keypress


    【解决方案1】:

    您应该使用 getModifiers 方法。

    if ((e.getKeyCode() == KeyEvent.VK_C) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) { 
    //Your code here }
    

    请注意,使用 & 运算符是因为它是位比较操作。

    【讨论】:

      猜你喜欢
      • 2010-10-24
      • 2013-03-21
      • 2021-10-22
      • 2016-09-20
      • 1970-01-01
      • 2021-02-25
      • 2013-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多