简体   繁体   English

管理我的 Java Swing 应用程序的快速关键速率,如何在管道的早期删除事件?

[英]fast key rates hosing my Java Swing application, how can I drop events early in the pipeline?

Some operating systems generate keyPressed events more quickly than my application can handle them.某些操作系统生成 keyPressed 事件的速度比我的应用程序处理它们的速度要快。 My first thought was to not perform the action if the calls are too close together, with something like this:我的第一个想法是,如果调用距离太近,则不要执行该操作,如下所示:

public void actionPerformed(ActionEvent e) {
    long now = System.currentTimeMillis();
    if(now - lastCall < 150) {
        System.out.println("dropping event");
    }
    else {
        lastCall = now;
    }
}

Unfortunately despite the event dropping, the application still locks up and builds up tons of events in the queue.不幸的是,尽管事件下降,应用程序仍然锁定并在队列中建立大量事件。 Is there a way I can drop events at a lower level?有没有办法可以在较低级别删除事件? I think by the time they get to my action listener above, it's too late to save the application from locking up.我认为当他们到达我上面的动作侦听器时,要使应用程序免于锁定为时已晚。 Thanks for your help!谢谢你的帮助!

EDIT: I should also mention that the listener is part of an AbstractAction that is associated with a menu item.编辑:我还应该提到侦听器是与菜单项关联的 AbstractAction 的一部分。

I'm not sure if this actually helps, but maybe you should do it like this:我不确定这是否真的有帮助,但也许你应该这样做:

public void actionPerformed(ActionEvent e) {
    e.consume();
    long now = System.currentTimeMillis();
    if(now - lastCall < 150) {
        System.out.println("dropping event");
    }
    else {
        lastCall = now;
    }
}

https://docs.oracle.com/javase/1.5.0/docs/api/java/awt/event/InputEvent.html#consume() https://docs.oracle.com/javase/1.5.0/docs/api/java/awt/event/InputEvent.html#consume()

If your app is unable to respond in time to keypresses, I would take the time-consuming section out-of-band and process in a separate thread.如果您的应用无法及时响应按键,我会将耗时的部分带外并在单独的线程中处理。

Then you can build a queue of keypresses and bin duplicates etc. if required, or perform other intelligent filtering on it.然后,如果需要,您可以构建一个按键队列和重复 bin 等,或对其执行其他智能过滤。 This is all application specific, but the core of it seems to be that you have heavy-duty processing in your GUI event processing.这都是特定于应用程序的,但其核心似乎是您在 GUI 事件处理中进行了繁重的处理。

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

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