On Thursday, June 1, 2017 at 8:06:30 AM UTC+8, andrew blinn wrote: > I'm trying to use a assign a keypress to toggle the display of the border of > an embedded editor-snip. Currently pressing the toggle key only toggles the > border of the topmost snip, whereas I want to toggle the border most local to > the current caret: > > ------------------------------------- > > #lang racket > (require racket/gui/base) > > ; setup frame > (define my-frame (new frame% [label "fructure"] > [width 400] > [height 400])) > (define my-canvas (new editor-canvas% [parent my-frame])) > (define my-board (new text%)) > (send my-canvas > set-editor my-board) > (send my-frame show #t) > > > ; override on-char to add a border toggle key > (define my-editor-snip% (class editor-snip% (super-new) > (define/override (on-char dc x y edx edy event) > (let ([key-code (send event get-key-code)]) > (match key-code > ['release void] > [#\e (send this show-border (not (send this > border-visible?)))]) > #;(super on-char dc x y edx edy event))))) > ; uncommenting duper call makes whole hierarchy toggle > > > ; make embedded editor > (define sub-board1 (new text%)) > (define sub-board1-editor-snip (make-object my-editor-snip% sub-board1)) > (send sub-board1 insert "sb1") > (send my-board insert sub-board1-editor-snip) > > ; make doubly embedded editor > (define sub-board1-1 (new text%)) > (define sub-board1-1-editor-snip (make-object my-editor-snip% sub-board1-1)) > (send sub-board1-1 insert "sb1-1") > (send sub-board1 insert sub-board1-1-editor-snip) > > > ; set caret focus to doubly embedded editor > (send sub-board1 set-caret-owner sub-board1-1-editor-snip 'global)
You need to check who has the focus before handing on-char events. Here is an updated my-editor-snip% that does what you want: (define my-editor-snip% (class editor-snip% (super-new) (define/override (on-char dc x y edx edy event) (define focus-snip (send (send this get-editor) get-focus-snip)) (if (and focus-snip (is-a? focus-snip editor-snip%)) (super on-char dc x y edx edy event) (let ([key-code (send event get-key-code)]) (match key-code ['release void] [#\e (send this show-border (not (send this border-visible?)))]) ))))) Best Regards, Alex. -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.