java 按下左键时如何检测鼠标移动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4443760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect mouse moving while left button down?
提问by Bnymn
I would like to detect mouse moving, while the left button is pressed.
我想检测鼠标移动,同时按下左键。
I simply create a drawing application. I can detect the mouse move without any mouse pressed. But I want to detect WITH left mouse pressed.
我只是创建了一个绘图应用程序。我可以在没有按下任何鼠标的情况下检测鼠标移动。但我想检测按下鼠标左键。
I think there is not any listener for this. So, what is the idea to do that?
我认为没有任何听众可以解决这个问题。那么,这样做的想法是什么?
采纳答案by Eternal Noob
回答by Jeff Storey
component.addmouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent evt) {
if ( SwingUtilities.isLeftMouseButton(evt)) {
// do your stuff here
}
}
});
回答by camickr
In your MouseMotionListener you can check for this using:
在您的 MouseMotionListener 中,您可以使用以下方法检查:
SwingUtilities.isLeftMouseButton(me.getPoint())
and you would listen for the mouseDragged event.
你会监听 mouseDragged 事件。
回答by DGH
I think there might be some sort of mouse drag listener, but if not...
我认为可能有某种鼠标拖动侦听器,但如果没有......
Capture the mouse button down and up events. In those events, set a boolean such as leftButtonIsDown = true
(unless there's already some easy way to query that) and then in the mouse movement events use a block like if (leftButtonIsDown)
捕获鼠标按下和按下事件。在这些事件中,设置一个布尔值,例如leftButtonIsDown = true
(除非已经有一些简单的方法可以查询),然后在鼠标移动事件中使用类似的块if (leftButtonIsDown)