links to this page:    
View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
Sensor vs. Hand
Last updated at 3:19 pm UTC on 14 January 2006
[Need to find: Squeak-3.7: Title-Bar Buttons discussion}
Question: Stephane Ducasse March 05, 2004 I need your help regarding the the recommendation to use ActiveHand instead of Sensor
 	^ lastMouseEvent anyButtonPressed
To give it a try, I added the above method in HandMorph and I changed my turtle code to be:
	(Delay forMilliseconds: 2) wait.
	^ ActiveHand anyButtonPressed
I put that in turtle just so that people do not have to type too much (Note: this is clearly bad but this is another discussion related to the problem I have with Morphic concurrency.) I played with scratch and I understand what you meant in a previous thread)
	"self new wand"
	[self anyButtonPressed]
		whileFalse: [self go: 30 atRandom.
			self turnLeft: 30 atRandom]
With this code, I cannot stop the turtle from looping when I press on shift or any key and mouse. If I removed the delay the squeak just blocks completely. While the loop stops as soon as I click with:
	(Delay forMilliseconds: 2) wait.
	^ Sensor anyButtonPressed
So I went down in the HandMorph I tried to register my turtle to event. In an inspector I did
	ActiveHand addKeyboardListener: self
But I do not know what do I get and how I should handle that. The methods says
 addKeyboardListener: anObject
	"Make anObject a listener for keyboard events. All keyboard
	events will be reported to the object."
But no more. So I looked into Object to see but it seems that the events mentioned there are related to the on:send:... one. So if you can enlighten me, Id appreciate it.

Answer: Vanessa Freudenberg This will not work (as you have discovered) since this piece of code runs on its own and does not give Morphic a chance to check for events, hence the button state will never change. You may have noticed that the rest of Squeak (or, more specifically, Morphic) is blocked while you execute this code.

Andreas did not say to never use Sensor. But certainly you should never refer to Sensor in well-behaved Morphic code. Your Turtle code is an example of simple "I-have-the-whole-machine-for-me" code, but it does not cooperate well with the event-driven logic of Morphic. There are several options for you:
a) Continue to use Sensor and accept that Morphic will block while your code is running.
b) Rewrite your example to follow the Morphic philosophy. This is certainly possible, but I doubt it will exhibit the same simplicity that the "old"code has.
c) Apply a little hacker-fu to make Morphic run under your control:
		World doOneCycle.		"Bert's magic hacker-fu"
		^ ActiveHand anyButtonPressed
This one works, since essentially Morphic runs the #doOneCycle method in a loop all day anyway. You have to be careful though to when you can call this, because I doubt Morphic is fully reentrant.
d) Write a system that allows and encourages the coding style you used (because it is so beautifully simple and almost obvious), but is actually event-driven under the hood. Your code would run quasi-parallel with all the other UI code. Tough stuff, though.
e) Do not write system d) yourself, but wait for Andreas to release it.

Answer: Andreas Raab You're hitting the wall with regards to what Morphic can do and what it cannot. IOW, your little example is precisely one of those "simple things" that I referred to which are so hard to do with Morphic. In such a situation your only choice is to go directly to Sensor.

With regard to the listeners, all events to the listener are sent via #handleListenEvent: (so this would be the place to start looking at) but notice that this wouldn't help you in the above. The hand simply won't get any events while you're "blocking" in the delay.

Question: Stephane Ducasse I always thought that it was bad to use World doOneCycle.

Answer: Vanessa Freudenberg It is bad. You should not teach that in your book. Teaching to use Sensor is far better in this context, especially if you mention that things are done differently under Morphic.