Hi Stephane,
On 15 May 2013 18:35, stephane ducasse <[email protected]> wrote:
> Hi eric
> Why do you need to poll for event?
>
I'd rather not poll as it seems inefficient, but the logic and ordering of
the Scratch commands seems to dictate that it is necessary.
Here's my example pseudo code from that picture of the Scratch script in my
original email...
When flag clicked [
Loop forever [
move 5 steps
turn 5 degrees
if key <space> pressed? [
Stamp the sprite on the screen
]
]
]
So "When flag clicked" is clearly event driven, and that's fine, that's how
it works already.
But as far as I can see, that "if key <space> pressed?" cannot be event
driven, because we don't want to start execution at that block if the space
key is pressed, rather we want that to evaluate to true if the space key
has been pressed recently ("recently" seems a problematic term to use here
but that's another issue!).
Here's the code I started throwing together to make this work -- see the
bit following this line:
type = EventTypeKeyboard
* InputEventSensor>>processEvent: evt *
"Process a single event. This method is run at high priority.
The event record is:
<type><timestamp><character code><updown><modifier keys>...
where updown is:
0 - keystroke
1 - key down
2 - key up
NOTE: You must ensure that there is an instance variable keyPressed."
| type updown |
type := evt at: 1.
"Treat menu events first"
type = EventTypeMenu
ifTrue: [
self processMenuEvent: evt.
^nil].
"Tackle mouse events first"
type = EventTypeMouse
ifTrue: [
"Transmogrify the button state according to the platform's button map
definition"
evt at: 5 put: (ButtonDecodeTable at: (evt at: 5) + 1).
"Map the mouse buttons depending on modifiers"
evt at: 5 put: (self mapButtons: (evt at: 5) modifiers: (evt at: 6)).
"Update state for polling calls"
mousePosition := (evt at: 3) @ (evt at: 4).
modifiers := evt at: 6.
mouseButtons := evt at: 5.
^evt].
"Finally keyboard"
type = EventTypeKeyboard
ifTrue: [
"Update state for polling calls"
modifiers := evt at: 5.
updown := evt at: 4.
* (updown = 2) ifTrue: [ *
* keyPressed := nil*
* ] ifFalse: [ *
* keyPressed := evt at: 3 ].*
^evt].
"Handle all events other than Keyborad or Mouse."
^evt.
Then I use this:
*InputEventSensor>>keyPressed: asciiValue*
"Is this key being pressed?"
self nextEvent.
^keyPressed = asciiValue
So should I be doing this? Should I test out these changes and suggest a
patch to Pharo? I'm happy that the answer might be "no", but what can I try
instead?
Thanks for your help.
-Eric.
--
Eric Clack
[email protected]
East Sussex, England.