6  HTML event handling

MysterX documents are interactive. You can trap mouse and keyboard events in documents, and use Scheme code to respond to the events.

Each HTML element in a document may have its own event handler, which is a procedure of one argument. An event handler needs to be registered with the document, as in

(send doc register-event-handler button (lambda (event) ...))

You also need to call

(send doc handle-events)

to begin event processing.

When an event occurs in an element, the event handler is called, passing an instance of the mx-event% class. You can test what kind of event occurred with the follow method predicates:
keydown? keyup? mousedown? mousemove? mouseover?
mouseout? mouseup? click? dblclick? error?

So an event handler that is looking for double-clicks would look like:

(lambda (event) (when (send event dblclick?) (do-something)))

For a given event, at most one of these predicates returns #t. All the other predicates return #f for that event.

Events also have attributes, that give you information about how the event occurred. Like the predicates, the attributes are methods associated with mx-event%. Some of the attributes are

We could extend the event handler above to print the coordinates of double-clicks:

(lambda (event) 
	(when (send event dblclick?) 
              (printf "Double-click at x = ~a, y = ~a~n"
                      (send event x) (send event y))))