12-21-2022 02:29 AM
Hi, can someone please explain me how event swallowing works, the purpose of it and how to use it in LabWindows/CVI. I have read the context help but couldn't wrap my head around it. Examples would be appreciated.
Solved! Go to Solution.
12-21-2022 05:52 AM
Hi,
user interface control elements such as buttons, menus etc. generate more than one event on user input. For example, if you click on a button, there will be an EVENT_LEFT_CLICK event, an EVENT_LEFT_CLICK_UP event, possibly a EVENT_GOT_FOCUS or an EVENT_COMMIT event.
Because callback functions respond to all events generated by the User Interface Library, the same callback function will be called multiple times. This may not always be ideal. In such a case, the callback function should return 1 instead of 0, telling CVI to forget / swallow all the other events of the related user action (here: clicking the button); in consequence no more callbacks will be called for this action.
An example:
/* This function MainCallbackEvents checks for all panel, control, and menu events. */
int CVICALLBACK MainCallbackEvents ( int panelOrMenuBarHandle, int controlOrMenuItemID, int event, void *callbackData, int eventData1, int eventData2 )
{
if ( event == EVENT_END_TASK )
{
...
return ( 1 );
}
else
{
....
return ( 0 );
}
}
12-21-2022 06:15 AM
To answer OP's question: in most cases just return 0 and don't think about it. The only times when you want to swallow an even is if you want to specifically block the cascade of events. For instance you can catch Ctrl-P in a panel to print the entire panel, or in a control to print only that control. In the latter case you'll swallow the keyboard event so as to block its transmission to the panel.
Which brings me to a question of my own: why don't we always return 1 on commit events ? It would probably save some useless loops. Is it because it's the last in the line of events (mouse move, focus, left click, left click up, val changed...) and it wouldn't matter anyway ?
12-21-2022 06:19 AM
Thank you now it makes sense.