简体   繁体   English

输入侦听器鼠标并同时按下键

[英]Listener mouse entered and key pressed at the same time

I have JFrame which contains JPanel. 我有包含JPanel的JFrame。 How to write listener to check when key(let it be SHIFT) is pressed and mouse enters to the JPanel area at the same time? 如何编写侦听器以检查何时按下键(使其为SHIFT)并且鼠标同时进入JPanel区域?

The MouseEvent provides information about the state of various elements, including some keys, via it's modifier properties, for example... MouseEvent通过其修改器属性提供有关各种元素的状态的信息,包括一些键,例如...

@Override
public void mouseEntered(MouseEvent e) {
    int modifiersEx = e.getModifiersEx();
    int onmask = MouseEvent.SHIFT_DOWN_MASK;
    if ((modifiersEx & onmask) == onmask) {
        // Shift key is down
    }
}

Will allow you to detect when the Shift key is pressed when the mouse enters a given component. 当鼠标进入给定组件时,将允许您检测何时按下Shift键。

Take a look at... 看一眼...

...for more details ...更多细节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM