[Cuis-dev] keyboard events in custom morph

Mark Volkmann r.mark.volkmann at gmail.com
Fri Aug 2 07:23:22 PDT 2024


On Thu, Aug 1, 2024 at 4:23 PM <ken.dickey at whidbey.com> wrote:

> On 2024-08-01 13:44, Mark Volkmann via Cuis-dev wrote:
>
> > I'm able to get mouse events in a custom morph, but I haven't been able
> > to do the same for keyboard events. Here is what I tried:
> >
> > I created a subclass of PlacedMorph.
> >
> > In the initialize method I have this:
> >
> > initialize
> > super initialize.
> > self setProperty: #handlesKeyboard toValue: true.
> >
> > In the keyDown: method I have this:
> >
> > keyDown: aKeyboardEvent
> > 'got keyDown' print.
> >
> > I create an instance of my custom morph in a Workspace and send it
> > #openInWorld to render it.
> > I then move the mouse over it and press a key, but I don't see output
> > in the Transcript.
> > Does anyone see what I'm missing?
>
> Yes.  You are forgetting to ask for #handlesKeyboard iMplementors.
>
> Morph>>processKeystroke: shows you need to supply a handler to
> #'keyStroke:'
>

The main thing I was missing was to get keyboard focus on mouse enter. I
needed to add the following instance methods:

handlesMouseOver: aMouseEvent
    ^ true.

mouseEnter: event
    (Preferences at: #focusFollowsMouse) ifTrue: [ event hand
newKeyboardFocus: self ].

mouseLeave: event
    (Preferences at: #focusFollowsMouse) ifTrue: [ event hand
releaseKeyboardFocus: self ].

-- 
R. Mark Volkmann
Object Computing, Inc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20240802/b8a3c216/attachment-0001.htm>


More information about the Cuis-dev mailing list