Events and event handling under the hood

前端 未结 4 675
执念已碎
执念已碎 2021-02-06 09:48

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

相关标签:
4条回答
  • 2021-02-06 09:52

    It is oldest resource but, by the way Java AWT/Swing event queue

    0 讨论(0)
  • 2021-02-06 09:55

    "...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.

    0 讨论(0)
  • 2021-02-06 10:05

    I'm not familiar with Qt, but in Java basically, yes. In practice it gets more complicated, with modal dialogs and the like, but basically it is getting the events from the underlying OS (for keyboard and mouse) and posting them as events on the EventQueue, and the various components get those events and use and consume them or pass them along as they wish.

    The listener framework allows the component to determine what the event was about and pass the appropriate information about it the appropriate listener.

    0 讨论(0)
  • 2021-02-06 10:09

    As said by answerers before, it is system specific, and could be implemented either way.

    If I remember the Windows API right (long years ago), there was a "Window function" which got called for every event, and I made a big switch to choose the right action depending on event type.

    I think most toolkits abstract this away, like the Java AWT with its event dispatch queue.

    For the X protocol, the input events (as well as "your window needs to be painted") come as event messages over the wire, and it is up to the toolkit library to convert this either in a application function call or add them to a queue to be queried later.

    The X protocol in fact works (basically) with two byte-streams: one from application ("client") to system ("server"), one in the other direction. The client sends request packages, the server sends back results (for some types of requests), errors (if some request could not be fulfilled for some reason), and events (when something happens on the display, like a mouse move, key press, window resize, ...).

    For local displays on modern systems this is usually implemented by some shared memory mechanism, but in principle this can go over TCP (or today mostly over SSH) for remote connections (thus "over the wire").

    I once started to create a pure Java X client implementation (didn't complete, as I found other more interesting things to do), and there I simply used a Socket with its InputStream and OutputStream to connect to the server. Thus, in principle I used the blocking native read() function of the Socket-InputStream to wait for events (and results and errors). I could have used a java.nio.SocketChannel in nonblocking mode, too, and then basically I would had a polling loop (doing other things between, of course). (Or I could use a selector, waiting until new readable data is there - thus blocking, too.)

    I have no idea what mechanism the AWT-for-X implementation used by the Linux and Solaris implementations of Java are using - I suppose they are based on some native toolkit, which in turn is based on the X client library (for C) "Xlib", but I don't know whether there is polling, blocking wait or being called by someone in the base.

    0 讨论(0)
提交回复
热议问题