简体   繁体   中英

Add click event handling to morph

I want to handle mouse click on a morph in an easy way. Can I do something like:

'hello' asMorph
  onClick: [ :e | "baboom" ];
  openInHand

?

Something like this?

StringMorph subclass: #ClickMorph
    instanceVariableNames: 'action'
    classVariableNames: ''
    category: 'MyMorph'

handlesMouseDown: evt
    ^true

mouseDown: evt
    evt hand waitForClicksOrDrag: self event: evt

mouseUp: evt
    action value

action
    ^ action

action: anObject
    action := anObject

You can call that as

(ClickMorph contents: 'Click me') 
    action: [Transcript open];
    openInWorld

The essentials are of course to have a block and to make sure you can react to a click.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM