[英]Simultaneous/Combined MouseListener & KeyListener, MouseEvent & KeyEvent Java
I want to know how work Simultaneously a MouseEvent & KeyEvent . 我想知道如何同时工作MouseEvent和KeyEvent 。
Suppose an JFrame, JPanel. 假设有一个JFrame,JPanel。
I want to know how work with an Event when certain MouseEvent & KeyEvent example: 我想知道在某些MouseEvent和KeyEvent示例时如何使用事件:
addKeyListener(new KeyAdapter() {
@Override public void keyReleased( KeyEvent evt) {
if ((evt.getKeyCode() == KeyEvent.VK_META) || (evt.getKeyCode() == KeyEvent.VK_WINDOWS)) {
//SomeEVENT
}
}
});
addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent evt) {
if (SwingUtilities.isLeftMouseButton(evt)) {
System.out.println("Left button released.");
}
}
});
TEST CODE : 测试代码 :
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
System.out.println("mouseClicked:");
if ((evt.getModifiers() & KeyEvent.VK_META) == KeyEvent.VK_META) {
System.out.println("KeyEvent.VK_META:");
}
if ((evt.getModifiers() & KeyEvent.VK_CONTROL) == KeyEvent.VK_CONTROL) {
System.out.println("KeyEvent.VK_CONTROL:");
}
System.out.println("KeyEvent.getKeyModifiersText(evt.getModifiers()):"
+ KeyEvent.getKeyModifiersText(evt.getModifiers()));
}
});
Output 输出量
mouseClicked:
mouseClicked:
mouseClicked:
mouseClicked:
mouseClicked:
The problem the Button Control is not detected!!! 未检测到按钮控件的问题!!!
But How perform the action only when simultaneosly LeftButton + CMD or WIN ? 但是, 如何仅在同时使用LeftButton + CMD或WIN时执行操作?
EDIT2 编辑2
Using my before TEST CODE and Pressing the Shift + Click, the Message is shown!!! 使用我之前的测试代码,然后按Shift +单击,将显示消息!
mouseClicked:
KeyEvent.getKeyModifiersText(evt.getModifiers()):⌥+Button1
mouseClicked:
KeyEvent.getKeyModifiersText(evt.getModifiers()):⌘+Button1
mouseClicked:
KeyEvent.getKeyModifiersText(evt.getModifiers()):⌃+Button1
mouseClicked:
KeyEvent.getKeyModifiersText(evt.getModifiers()):⌘+⌥+Button1
mouseClicked:
KeyEvent.getKeyModifiersText(evt.getModifiers()):⌘+⌥+Button1
mouseClicked:
KeyEvent.getKeyModifiersText(evt.getModifiers()):⌥+Button1
mouseClicked:
KeyEvent.getKeyModifiersText(evt.getModifiers()):⌘+⌥+Button1
mouseClicked:
KeyEvent.getKeyModifiersText(evt.getModifiers()):Button1
mouseClicked:
KeyEvent.VK_CONTROL:
KeyEvent.getKeyModifiersText(evt.getModifiers()):⇧+Button1
Is it a OS Problem (I have macOS Sierra )? 是操作系统问题(我有macOS Sierra )吗?
The answer is a simple as reading the JavaDocs for MouseEvent
答案很简单,就是阅读适用于MouseEvent
的JavaDocs
The MouseEvent
carries with it an Extended Modifier state which is a simple bit mask of the buttons and keys which were used to generate the event. MouseEvent
带有扩展修改器状态 ,该状态是用于生成事件的按钮和键的简单位掩码。 There are plenty of examples about of how to make use of them. 关于如何利用它们,有很多示例。
Because I'm both stupid and lazy, I just prefer to use the functionality provided by the event object itself, like MouseEvent#isControlDown
and MouseEvent#isMetaDown
因为我既傻又懒,所以我只喜欢使用事件对象本身提供的功能,例如MouseEvent#isControlDown
和MouseEvent#isMetaDown
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.