💾 Archived View for blitter.com › apl-books › APLX50 › APLX-manual › www.microapl.com › apl_help › c… captured on 2023-01-29 at 14:37:38.
⬅️ Previous capture (2022-07-17)
-=-=-=-=-=-=-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Events and Callbacks</TITLE> <META NAME="DESCRIPTION" CONTENT="APL language help page: Events and Callbacks"> <META NAME="KEYWORDS" CONTENT="event,callback,apl,aplx,apl help"> <!-- %%COMMON_HEAD%% --> <META http-equiv="Content-Type" content="text/html; charset=utf-8"> <LINK rel="stylesheet" type="text/css" href="http://www.microapl.com/styles_apl_help.css"> <!-- %%END%%--> </HEAD> <body> <table> <tr> <td width="800" valign="top" colspan="2"> <center>Topic: <A HREF="ch.htm"><code>APLX Help</code></A> : <A HREF="ch_030.htm"><code>System Classes</code></A> : <A HREF="ch_030_050.htm"><code>Events and callbacks</code></A> : <A HREF="ch_030_051.htm"><code>Introduction to Events and Callbacks</code></A> </center> <center> [ <A HREF="ch_030_055.htm">Next</A> | <A HREF="ch.htm">Contents</A> | <A HREF="help_index.htm">Index</A> | <A HREF="http://www.microapl.co.uk/apl/index.html">APL Home</A> ]</center> <br></td> </tr> <tr> <td width="120"> <a href="http://www.microapl.co.uk/apl/index.html"><img height="68" border="0" width="119" src="MicroAPL_logo.gif" alt="www.microapl.co.uk"></a> </td> <td align="left" valign="bottom"> <h1>Events and Callbacks</h1> </td> </tr> <tr> <td width="800" valign="top" colspan="2"> <hr> <H2>Concepts </H2> <P>Windowing systems such as MacOS and Windows are event-driven, which means that your program has to be able to respond to events such as a key stroke or new data being available. An event (sometimes called a message) can arise from a number of sources. It can be directly triggered by a user action <code>-</code> for example, Mouse Down or Key Down. It might be indirectly triggered by a user action <code>-</code> for example, if the uses presses the mouse button over a button in a dialog, a Mouse Down event occurs, and this in turn causes a Clicked event for the button. Events can also be triggered by the system software <code>-</code> for example, to indicate that a window needs to be redrawn <code>-</code> or by another program or even your own program.</P> <P>Much event handling is automatic, and you do not normally need to deal with it. For example, redrawing of windows is usually handled automatically by APL. For events which you do need to know about, such as a Click event in a button, you define an APL <i>callback</i>, which is an APL expression (usually a function) which will be run if the event occurs. You assign a character vector containing the expression or function name to the appropriate callback property of the object. This is a special property (whose name begins with 'on..') which creates the association between the event and your callback function or expression. </P> <p>For example, this code creates a dialog with an OK button, and associates the APL function <tt>ENDDIALOG</tt> with the Click event for the button:</p> <pre> DEMO←'⎕' ⎕NEW 'Dialog' DEMO.title←'Button Example' DEMO.OK.New 'Button' DEMO.OK.onClick←'ENDDIALOG' </pre> <P>The APL callback function is actually run during execution of the system function <A HREF="ch_020_070_860.htm"><code>⎕WE</code></A> (wait for event).</P> <H2>How APLX handles events </H2> <P>When an event occurs, APLX looks to see if the event refers to a user-defined window, or to one of APL's own windows (such as a session or edit window). In the latter case, the event is handled internally and you do not normally need to be aware of it. If the window was user-defined, however, then your APL application might need to be informed of the event. For every event, there is a default system action (which may be to do nothing), and there might in addition be an APL callback function which you have defined for that event. Often, the default system action is to create another, higher-level event. At the lowest level, events take place in windows, but where appropriate are passed to a control (such as an Edit text or Check box) to produce a higher-level event.</P> <H2>When events are handled </H2> <P>APLX calls the operating system for the latest event at two main points in the code. These are:</P> <P>(a) When APL is requesting input<BR></P> <P>(b) During execution, the APL interpreter many times a second to see if any events need to be handled.</P> <P>The system handler for an event is invoked immediately the event is retrieved from the operating system. This means that events continue to be processed even if your application is busy (doing a long calculation, for example), which helps APLX applications appear responsive to window updates and user actions. In contrast, your APL callback functions are executed only when you call <code>⎕WE.</code></P> <P>Where you have defined an APL callback for an event, the event is placed in a queue, and callbacks in the queue are run in turn when your program next calls <code>⎕WE.</code> You can either call <code>⎕WE</code> in an 'event loop' (with a long or infinite timeout value), in which case all processing takes place by <code>⎕WE</code> looping internally and executing your callback functions as events happen, or you can call <code>⎕WE</code> with a small or zero timeout value, in which case it will return almost immediately and you can continue with other APL processing. Where your program needs to carry out processing which will take a long time, it should call <code>⎕WE</code> from time to time (preferably at least two or three times a second), bearing in mind that when it does so events may cause other callbacks to be invoked.</P> <P>Note that, when APL is in desk-calculator mode (for example, if an untrapped APL error occurs in your program), your APL callback functions will not be run. When you next call <code>⎕WE,</code> there may be quite a few callbacks queued up. You can flush these from the event queue by passing a left argument of 1 to <code>⎕WE.</code></P> <H2>Information about events</H2> <p>When your APL callback function runs, you can find out information about the event using the niladic system function <code><A HREF="ch_020_070_290.htm">⎕EV</A></code>. This returns the system clock, keyboard state and mouse position at the time of the event, a number which identifies the event type, and sometimes extra information associated with a particular type of event.</p> <hr> </td> </tr> <tr> <td width="800" valign="top" colspan="2"> <center>Topic: <A HREF="ch.htm"><code>APLX Help</code></A> : <A HREF="ch_030.htm"><code>System Classes</code></A> : <A HREF="ch_030_050.htm"><code>Events and callbacks</code></A> : <A HREF="ch_030_051.htm"><code>Introduction to Events and Callbacks</code></A> </center> <center> [ <A HREF="ch_030_055.htm">Next</A> | <A HREF="ch.htm">Contents</A> | <A HREF="help_index.htm">Index</A> | <A HREF="http://www.microapl.co.uk/apl/index.html">APL Home</A> ]</center> <br></td> </tr> </table> <!-- %%COMMON_BODY_TAIL%% --> <p class="copyright">Copyright © 1996-2010 MicroAPL Ltd</p> <!-- %%END%% --> </body> </html>