Events: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed indentations.)
(Lingo added)
Line 507: Line 507:
</pre>
</pre>
OTP comes with a <code>gen_even</code>t behavior that is more robust and resilient than this version. That is what should be used for any non-toy example or project.
OTP comes with a <code>gen_even</code>t behavior that is more robust and resilient than this version. That is what should be used for any non-toy example or project.

Lingo/Director uses (stateless) events for system/application state change notifications, user action notifications and inter-sprite communication.

To catch an event, a corresponding event handler - a function with a predefined name - has to be definined in the code. Examples for such event handlers are:
<lang lingo>-- the current window was closed
on closeWindow
...
end

-- the left mouse button was pressed by the user
on mouseDown
...
end</lang>
Also "Sprites" (visual elements) receive events by setting up such event handlers in scripts attached to them. Both predefined and custom events can be sent programmatically to sprites, e.g. using:
<lang lingo>-- send event #mouseDown programmatically to sprite 1
sendSprite(1, #mouseDown)

-- send custom event #foo to named sprite "bar"
sendSprite("bar", #foo)

-- send a custom event #fooBar to all existing sprites
sendAllSprites(#fooBar)</lang>

Using a binary plugin ("Xtra"), in Windows also lower level window messages can be both sent and received:
{{libheader|Msg Xtra}}
<lang lingo>mx = xtra("Msg").new()

-- send message WM_LBUTTONDOWN to a specific window identified by HWND hwnd
WM_LBUTTONDOWN = 513
MK_LBUTTON = 1
lParam = 65536*y + x
mx.send_msg (hwnd, WM_LBUTTONDOWN, MK_LBUTTON, lParam)

-- listen for WM_COPYDATA and WM_MOUSEWHEEL messages sent to current application
-- window, notify Lingo callback function 'msgReceived' when such messages occur
WM_COPYDATA = 74
WM_MOUSEWHEEL = 522
mx.msg_listen([WM_COPYDATA, WM_MOUSEWHEEL], VOID, #msgReceived)</lang>


=={{header|JavaScript}}==
=={{header|JavaScript}}==