[Newbies] Capture keyboard focus for real and true.

Bert Freudenberg bert at freudenbergs.de
Mon Jul 2 23:56:07 UTC 2007


On Jul 3, 2007, at 1:23 , Blake wrote:

> On Mon, 02 Jul 2007 16:06:08 -0700, Bert Freudenberg  
> <bert at freudenbergs.de> wrote:
>
>> Send #addKeyboardListener: with your morph as argument to the  
>> Hand. This way you get all keyboard events independent ot keyboard  
>> focus.
>
> Well, that's what I thought. In the words of Randy Newman, maybe  
> I'm doing it wrong. In my morph class I have:
>
> initialize
>    super initialize.
>    xDir _ 0.
>    yDir _ 0.
>    ActiveHand addKeyboardListener: self; keyboardFocus: self.
>
> which is the only reason it works at all. In my keydown method, I  
> have:
>
> keyDown: evt
>    self halt.
>    (evt keyCharacter = $W) ifTrue: [xDir _ 0. yDir _ -1].
>    (evt keyCharacter = $S) ifTrue: [xDir _ 0. yDir _ 1].
>    (evt keyCharacter = $D) ifTrue: [xDir _ 1. yDir _ 0].
>    (evt keyCharacter = $A) ifTrue: [xDir _ -1. yDir _ 0].
>    ^evt
>
> I put in the halt just in case the code was wrong somehow, but if I  
> click on any text window, messages stop going to keyDown for my morph.

Works for me (though you actually do not need the #keyboardFocus: send).

Note that in Morphic some parts only work with keyStroke events. And  
keyDown/Up events are pretty much neglected, you find almost no  
examples with them.  Besides, up/down codes are platform dependent  
(on my Mac W,A,S,D gives keyValues 13, 0, 1, 2, but they will give  
these codes independent of the keyboard setting so you can be sure it  
refers to the same physical keys).

- Bert -




More information about the Beginners mailing list