简体   繁体   English

在Java中,什么事件*在鼠标按钮下连续*被触发?

[英]In Java, what event is *continuously* fired on mouse button down?

I would like to know if there exists in Java an event that is continuously fired when a mouse button is being held down (pressed), even if the mouse is not moved. 我想知道Java中是否存在按下(按下)鼠标按钮时不断触发的事件,即使鼠标未移动也是如此。 I could not find it in typical MouseListeners: 我在典型的MouseListeners中找不到它:

  • MouseDragged is fired only if the user move the mouse while holding the button down 仅当用户在按住按钮的同时移动鼠标时才会触发MouseDragged
  • MousePressed is fired only once when the button has been pressed 当按钮按下时的mousePressed只发射一次

And that's about it. 这就是它。 Any idea how to make such an event? 知道如何制作这样的活动吗?

Cheers 干杯

jy JY

There is an obvious reason why this event is not available in MouseListener: it's going to spam you with events such that everything is slowed down to a halt. 有一个明显的原因,为什么这个事件在MouseListener中不可用:它会向你发送垃圾邮件,事件是一切都放慢了。 Do you want to receive this event every second, every ms, or even more often? 你想每秒,每毫秒,甚至更频繁地接收这个事件吗? If you need that you'll have to do it yourself. 如果你需要,你必须自己做。

For stearing this process you of course need mousePressed and mouseReleased to determine whether a button is currently held down. 对于这个过程,您当然需要使用mousePressedmouseReleased来确定当前是否按下按钮。 Then you need to run some kind of loop that generates the corresponding events you'd like to have. 然后你需要运行某种循环来生成你想要的相应事件。

You might also want to work via polling, ie extend your MouseListener class such that it can tell you whether a button is still held down, and whereever you need those events you can actively poll for the button. 您可能还希望通过轮询工作,即扩展您的MouseListener类,以便它可以告诉您按钮是否仍然按下,并且您需要那些可以主动轮询该按钮的事件。 It depends on how you want to use these events, which approach is more suitable. 这取决于您希望如何使用这些事件,哪种方法更合适。

If you need to do something whilst the mouse button is down, just start it when you detect a mousePressed event and then continuously do that until you detect a mouseReleased event. 如果您需要做的事情,而鼠标按键时,当检测到刚刚启动mousePressed事件,然后继续这样做,直到你检测mouseReleased事件。 Then you don't need to have your event continuously firing. 然后你不需要让你的事件持续开火。 eg 例如

public void mousePressed(MouseEvent e) {
    someCondition = true;
    while(someCondition) {
        //do something
    }
}

public void mouseReleased(MouseEvent e) {
    someCondition = false;
}

EDIT: As others have said, the code would need to be separated from the event thread otherwise the call to mouseReleased would get blocked preventing the loop from ending. 编辑:正如其他人所说,代码需要与事件线程分开,否则对mouseReleased的调用将被阻止,阻止循环结束。

James Goodwin's code will not work. James Goodwin的代码不起作用。 mousePressed and mouseReleased are both fired from the GUI thread, so blocking in mousePressed will prevent mouseReleased from ever being fired, meaning the loop will continue forever. mousePressed和mouseReleased都是从GUI线程触发的,因此mousePressed中的阻塞将阻止mouseReleased被触发,这意味着循环将永远继续。

If you already have a seperate thread for processing then use mousePressed to indicate to that thread that the event should start, and mouseReleased to stop. 如果您已经有一个单独的线程进行处理,那么使用mousePressed向该线程指示该事件应该开始,并且mouseReleased停止。

If you don't have a separate thread and don't want the hassle, a Timer is probably the easiest way. 如果你没有单独的线程并且不想要麻烦,那么Timer可能是最简单的方法。 javadoc on Timer . 定时器上的javadoc

Specifically, you should create a TimerTask that does whatever it is you want to do multiple times and queue it using Timer.schedule: 具体来说,您应该创建一个TimerTask,它可以执行多次您想要执行的操作,并使用Timer.schedule对其进行排队:

Timer timer = new Timer();
TimerTask task = new MyTimerTask();

private class MyTimerTask extends TimerTask {
    public void run() {
        // your code here
    }
}

public void mousePressed(MouseEvent e) {
    timer.scheduleAtFixedRate(task, 0, 1000); // Time is in milliseconds
    // The second parameter is delay before the first run
    // The third is how often to run it
}

public void mouseReleased(MouseEvent e) {
    task.cancel();
    // Will not stop execution of task.run() if it is midway
    // But will guarantee that after this call it runs no more than one more time
}

I'm pretty sure this is the simplest way, as it doesn't involve faffing around with inter-thread communication. 我很确定这是最简单的方法,因为它不涉及线程间通信。

Oh, and as Peter said, you will have to add code to account for the user mousing down on your app and mousing up somewhere else. 哦,正如彼得所说,你必须添加代码来考虑用户将鼠标放在你的应用程序上并将鼠标放在其他地方。

There is no such event 没有这样的事件

You can create your own by starting a timer in the mousedown method and ending the same timer in the mousereleased 您可以通过在mousedown方法中启动计时器并在鼠标释放中结束相同的计时器来创建自己的计时器

You will also need a few fail saves to make sure the timer stops when you do mousedown the movemove onto another component or even onto other frame or non java gui parts 您还需要进行一些失败保存,以确保当您将移动移动到另一个组件或甚至移动到其他框架或非Java gui部件上时计时器停止

I think you'll find the answer is no, there is no such event. 我想你会发现答案是否定的,没有这样的事件。 Not just for Java, but for any GUI framework. 不仅适用于Java,还适用于任何GUI框架。 It would serve no purpose other than to tie up the event queue. 除了绑定事件队列之外,它没有用处。

You would need to create your own by trapping the mouse down event then having a timer fire events at regular intervals until the mouse up event. 您需要通过捕获鼠标按下事件来创建自己的事件,然后定期触发事件,直到鼠标按下事件。

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

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