<div dir="ltr">Thanks for the further clarification. It seems this is what I really need in order to evaluate a block of code each time a key is pressed in the TextModelMorph:<div><br></div><div><font face="monospace">input := TextModelMorph withText: 'edit me'.<br>input keystrokeAction: [ :evt | evt keyValue print ].<br>input openInWorld.</font><br></div></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Mon, Jan 13, 2025 at 10:02 PM Weslleymberg Lisboa <<a href="mailto:weslleym.lisboa@gmail.com">weslleym.lisboa@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">Em 13/01/2025 22:29, Mark Volkmann escreveu:<br>
> Thanks so much for this tip! Clearly a key to getting this to work is <br>
> knowing the symbols that the watched object passes to the changed: <br>
> method. I see that the TextModel instance method actualContents: uses <br>
> the symbol #actualContents. So I tried the following to test whether I <br>
> could make a sound every time the text in a TextModelMorph is modified:<br>
> <br>
> Preferences at: #soundsEnabled put: true.<br>
> model := TextModel withText: 'Hello'.<br>
> model when: #actualContents send: #primitiveBeep to: Smalltalk.<br>
> input := TextModelMorph withModel: model.<br>
> input openInWorld.<br>
<br>
If you look the implementation of #changed: you will see that it <br>
triggers a #changed: event passing along whatever symbol it received as <br>
argument. This should print 'actualContents' in the Transcript:<br>
<br>
model := TextModel withText: 'Hello'.<br>
model when: #changed: send: #log: to: Transcript.<br>
model actualContents: 'Hello!'.<br>
<br>
It won't work with #primitiveBeep because the #changed: event always <br>
comes with an argument. But the following should produce a sound:<br>
<br>
model := TextModel withText: 'Hello'.<br>
model when: #changedContents send: #primitiveBeep to: Smalltalk.<br>
model triggerEvent: #changedContents.<br>
<br>
<br>
> I don't hear sounds when modifying the value. I thought perhaps the <br>
> problem is that the TextModel instance method actualContents: is not <br>
> called every time the value is modified. But even when I explicitly <br>
> invoke it with model actualContents: 'Test', I don't hear a sound. I do <br>
> hear a sound when I evaluate Smalltalk primitiveBeep.<br>
<br>
In fact, TextModelMorph never calls #actualContents:. This is <br>
interesting. I did some digging and following a key stroke the Text <br>
instance that TextModel holds will be modified and the morphs involved <br>
in the process will redraw. So the "Observer Pattern events" we are <br>
trying to catch here will only happen when the TextModel changes for <br>
some reason outside the event processing (mainly keyboard and mouse <br>
events) done by TextModelMorph.<br>
<br>
In summary, if a change in the model is caused by an interaction with <br>
TextModelMorph, the Morph knows about the change and will update itself. <br>
But it has no way to know if the model changes by other means. This is <br>
where the Observer Pattern comes into play.<br>
<br>
I just learned a lot trying to understand all this :)<br>
<br>
-- <br>
Wéslleymberg Lisboa<br>
Graduado em Sistemas de Informação<br>
Docente no IFFluminense - Campus Itaboraí<br>
<br>
</blockquote></div><div><br clear="all"></div><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div><font face="arial, helvetica, sans-serif">R. Mark Volkmann</font></div><div><span style="font-size:12.8px"><font face="arial, helvetica, sans-serif">Object Computing, Inc.</font></span></div></div></div></div></div></div></div></div>