Question

I'm working on Phratch (Scratch 2.0 in Smalltalk) and have modified the InputEventSensor>>processEvent method to store the last key pressed, in a similar manner to how the modifier keys are stored, so that they if <key> pressed blocks work.

So I now have a customised InputEventSensor class:

  • new instance variable keyPressed
  • modified method processEvent
  • new methods keyPressed and clearKeystate

The new methods are in a category *Phratch which is fine, so these get saved to the Phratch Monticello package. But what about processEvent, should that be moved to the *Phratch category? What about the instance variable?

Was it helpful?

Solution

There are several things going on :).

First, moving an existing method to your project's package is what we call an override. That is, when you load your package into the image, the existing method definition will be overriden by your definition. I wouldnt advice to make overrides only when extremely necessary. Actually, the best approach should be to create hooks, integrate them in Pharo's codebase, and then plug your project in the given hook :).

On extending classes with instance variables, I don't think Monticello handles that really. A possible solution is to add a pre-load action in a metacello configuration, adding the instance variable to the given class. That way, the inst var is installed before the methods.

Third and last, the actual good way to know if some key is still pressed, is to listen to the KeyDown and KeyUp events. A key is released when a KeyUp event is given. However, keyDown and keyUp events are not properly working on Pharo right now (in all platforms, the keycodes to identify the keys yield OS specific values, and there are some keys that are not well handled yet). But, I'm confident this will get better in the upcoming 3.0 version of Pharo.

Of course, you can contribute if you want to make any of this points better :).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top