事件和事件处理 [英] Events and event handling under the hood

查看:112
本文介绍了事件和事件处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚通过一些简单的程序使用SDL,它让我想起了我写的一些Java GUI。



在简单的SDL程序我有一个循环检查任何事件(按键,鼠标点击等),并对它们做出反应。基本上它是投票输入。



在Java中,您将侦听器附加到GUI对象,并且在某些事件发生时听众被触发。我的问题是,Java是否在我们的后台处理这个轮询循环,并且处理像点击GUI控件那样可以触发正确的监听器的事情,还是有更复杂的事情呢?我知道Qt有一个与Java类似的事件系统,您可以使用插槽将处理程序连接到GUI控件。这也只是处理所有的投票和制定哪个控件被点击了我们?还是再来一些更复杂的事情呢?



更新



也许我还不够清楚问题。我真的很想知道事件如何桥接OS层 - 应用层边界。应用程序是否轮询OS层并将事件信息拉入应用程序?或者操作系统有一些方法来中断/通知应用程序发生事件,并将事件信息推送到应用程序。



已经向我推荐了第三个解决方案应用程序调用一个阻塞本机函数,如:

 事件e = someNativeFunction(); // block until someNativeFunction()返回一个事件


解决方案

... Java在后台为我们处理这个轮询循环??



很多,是的。 UI系统为程序提供对包含已发生的事件和目标的队列的访问。程序运行一个循环,从该队列中请求项,然后执行任何操作。像Qt和Java这样的库可以在窗口小部件类中调用特殊功能,告诉他们发生了一个事件,以便系统可以从那里起作用,但是由API规定。图书馆必须从系统特定的窗口ID转换为管理该小部件的类才能执行。



Qt以受保护的形式提供对此功能的访问,虚拟 onXxxxEvent()函数。许多窗口小部件的标准行为是响应于事件生成信号,但这些事件始终是特定窗口小部件特定事件的一些翻译形式。 Qt为您提供访问权限,以便您可以覆盖窗口小部件处理事件的方式,或为之前(通过子类化)的事件添加其他行为。



每个UI系统略有不同,但在我的经验中,它们都以这种方式基本相同。我们正在谈论原始的win32和Xlib。


I've just been working through some simple programs using SDL and it made me think back to some Java GUIs I've written.

In the simple SDL programs, I have a loop that checks for any events (keypresses, mouse clicks etc...) and reacts to them. Essentially it's polling for input.

In Java, you attach listeners to the GUI objects and the listeners get triggered when certain events occur.

My question is, does Java just handle this polling loop in the background for us and work out things like which GUI control was clicked so that it can trigger the correct listener, or is there something more complex going on?

I know Qt has a similar event system to Java where you use slots to connect a handler to a GUI control. Is this also just handling all the polling and working out which control was clicked for us? Or again, is there something more complex going on?

UPDATE

Maybe I wasn't clear enough with the question. I'm really looking to find out how an event bridges the OS Layer - Application layer boundary. Does the application poll the OS layer and pull event information into the application? Or does the OS have some way to interrupt/notify the application that an event has occurred and push the event info to the application.

A third solution has been suggested to me that the application calls a blocking native function like:

Event e = someNativeFunction(); // blocks until someNativeFunction() returns an event

解决方案

"...does Java just handle this polling loop in the background for us...?"

Pretty much, yes. The UI system provides the program with access to a queue that contains events that have occurred and the target. The program runs through a loop requesting items from this queue and then does whatever. Libraries like Qt and Java call special functions within the widget classes that tell them an event has happened so that the system can function from there, however stipulated by the API. The library has to translate from the system specific window ID to the class governing that widget in order to do so.

Qt provides access to this function in the form of protected, virtual onXxxxEvent() functions. The standard behavior of many widgets is to generate signals in response to events, but these are always some translated form of the event specific to the particular widget. Qt provides you access to this so that you can override the way a widget handles an event or to add additional behavior for events it never listened to before (through subclassing).

Each UI system is slightly different but they are all basically the same in this manner in my experience. We're talking raw win32 and Xlib here.

这篇关于事件和事件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆