[Newbies] Handling keypresses in Morphic.
blake at kingdomrpg.com
Fri Jun 22 10:25:10 UTC 2007
I'm trying to figure out the best way to--well, this is probably
blasphemy but--use Squeak as I would a more traditional development
environment. To wit: Normally, when coding, I code, compile if necessary,
then run. At that point, my development environment fades and I can focus
on the results.
In this situation, I'm writing morphic objects and I want to be able to
control these through the keyboard. I think I need to use
#handlesKeyboard, #keyDown, etc., but have found that not to work. And
rather annoyingly, since I'm running things from the transcript, what
usually happens is I wipe out my transcript code by typing without
realizing it's still focussed.
But then I thought, well, focus floats with the mouse anyway, so I
probably don't want to design it so that the individual morphics respond
to keystrokes (think "game" rather than "businss app"). Better to capture
the keystrokes and convert them into commands that get sent to the various
So I create a morph that acts as a command center, and in the initialize
I do this:
ActiveHand addKeyboardListener: self; keyboardFocus: self.
which makes it so that the command morph gets the keyboard commands and is
set to have the focus. This initially caused some trouble since I
apparently also needed to implement handleListenEvent. Because I didn't I
got into a pickle where I couldn't use Squeak for all the DNUs that were
popping up. This fixed it:
That's the whole thing. I'm not sure why I need to handle the generic when
I'm only interested in the key events.
Now I want to handle events, starting with cursor keys. When I look at
the up key in the debugger, it looks, rather pleasingly like this:
but obviously I can't test for that:
(evt = [keyDown '<up>']) ifTrue: ...
Actually, that wasn't obvious. I tried it and it didn't work. So, do I
have to test for the key code?
Thanks for any thoughts!
More information about the Beginners