[Vm-dev] Re: [squeak-dev] Oddities with #keyDown and Sensor

Bert Freudenberg bert at freudenbergs.de
Fri Sep 23 20:59:15 UTC 2011

On 23.09.2011, at 12:15, Lawson English wrote:

> I've been playing with creating a simple 2D asteroids type game in Squeak, and when it came time to try to monitor how long the accelerator key for the rocket (up arrow in this case) had been pressed, I ran into all sorts of oddities.
> On an intel Mac OS X 10.6.8 box, running various Cog VMs, I found that the event passed to #keyDown: contained zero as the value, regardless of which key was pressed.
> On Linux, this problem seems not to exist.
> Again on the Mac, this test class http://paste.lisp.org/display/124816 doesn't seem to work.
> I was trying to grab the raw Sensor event buffer, but I can't seem to get more than a handful of events.
> #step is where the meat of the code is. It tries to grab events from Sensor and if they are keyboard events, put them into a buffer for examination.
> You evoke the test morph thusly:
> myMorph := MyMorph new.
> myMorph  openInWorld.
> Transcript show: myMorph aCounter.
> Transcript show: myMorph eventBuffer.
> Obviously, I'm doing something fundamentally wrong

Yes. Do Not Use Sensor Directly From Morphic Code. Otherwise you get unpredictable behavior because the Morphic main loop also fetches events from Sensor.

That said, keyboard events are indeed inconsistent across platforms, and you have to make do with whatever the particular platform offers. Be sure to look at both the 8-bit char code (evtBuf at: 3) and the 32 bit unicode (evtBuf at: 6) - some keys may be reported only in one of the two places. A good strategy is to look at (evtBuf at: 6) and if it is 0 use (evtBuf at: 3).

Note that on the Mac, keydown/up events use different codes than stroke events.

- Bert -

More information about the Vm-dev mailing list