[squeak-dev] Some beginner questions about the EventHandler of a PolygonMorph

Marcel Taeumel marcel.taeumel at hpi.de
Thu Feb 9 07:10:16 UTC 2023

Hi Eduardo --

Looks good so far. :-)

User-input events are generated from data arriving from the VM:

HandMorph >> #processEvents
HandMorph >> #generateKeyboardEvent:

Such an event is either sent to the current focus holder or just dispatched normally starting in the world:

HandMorph >> #handleEvent:
HandMorph >> #sendKeyboardEvent:
HandMorph >> #sendMouseEvent:

You can change the current focus holder via:

anEvent hand newKeyboardFocus: anObject.
- or -

self currentHand newKeyboardFocus: anObject.

- or -

self currentEvent hand newKeyboardFocus: anObject.

Note that #on:send:to: (in EventHandler) allows you to access the actual event object if you have a callback with at least 1 argument. Also note that you can just send #on:send:to: to any morph. It will install an #eventHandler if not present.

The overall event dispatching is encoded here:

Morph >> #processEvent:using:
MorphicEventDispatcher >> #dispatchEvent:with:

In general, there is first a "capturing phase" that does down the scene tree and the hand's position (i.e., mouse coordinates) and then a "bubbling phase" that goes up the target's (i.e., morph's) owner. Any focus holder will just change the starting point of such event dispatching ... more or less :-)

There is more info on that on the squeak-dev mailing list. Just click "Tools > Squeak Inbox Talk" and search the mails from about the last 3-4 years.

Hope this helps. Happy squeaking!! :-)


Am 09.02.2023 07:32:48 schrieb Eduardo Ochs <eduardoochs at gmail.com>:
Hi list,

I asked this question here on the list a few days ago,


on how to write code that _sends_ certain keyboard events. I think
that I got a prototype of an answer for it, that "just" needs some
debugging... and then I started to work on ways to debug my prototype.

The ideal way to test sending keyboard events would be to have some
code that _receives_ and _handles_ keyboard events, and that runs
things like "Transcript show: evt" or "evt halt" when it receives a
keyboard event. I tried to adapt the code from these wiki pages

https://wiki.squeak.org/squeak/1125 Programming Morphs (Active Essay)
https://wiki.squeak.org/squeak/1216 Programming morphs - page 7 -
keyboard events
https://wiki.squeak.org/squeak/3503 Morphic Key Events

without much success, and I also tried to modify the keyboard
exerciser - i.e., this:

"World menu -> objects (o) -> Categories -> Demo -> Exercise Keyboard"
KeyboardExerciser new openInWorld.

...and then I saw a way to play with keyboard handlers that does not
require creating any classes or modifying any methods, and that can be
run entirely from a workspace with "do it"s and "print it" in the
right places. It is here:

"The first lines were adapted from:
https://wiki.squeak.org/squeak/2188 PolygonMorph"

a := Array with: 50 at 100 with: 200 at 300 with: 100 at 400.
b := (PolygonMorph
vertices: a color: Color red
borderWidth: 1 borderColor: Color black).
World addMorph: b.
b beSmoothCurve.
b beStraightSegments.

b vertices.
b vertices at: 1.
b vertices at: 1 put: 40 at 100.
b vertices at: 1 put: 50 at 100.
b vertices at: 1 put: 40 at 100. b computeBounds.
b vertices at: 1 put: 50 at 100. b computeBounds.

a at: 1 put: 40 at 100. b computeBounds.
a at: 1 put: 50 at 100. b computeBounds.

b color.
b color: Color banana.
b color: Color fern.
b color: Color red.

e := EventHandler new.
b eventHandler.
b eventHandler: nil.
b eventHandler: e.

Now how do I change that event handler in interesting ways?
Does this make sense?

e on: #keyStroke send: #halt to: b.

Also, is there a way to transfer the keyboard focus to the
PolygonMorph with a "do it" in that workspace? I guess that we
need to run the method #newKeyboardFocus:, but how exactly?
Anyone has examples to share?

Thanks in advance,
Eduardo Ochs

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20230209/406b42a6/attachment.html>

More information about the Squeak-dev mailing list