[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: basic view-key-event-handler confusion



>Please help.
>
>There is something *very basic* that I don't understand about 
>view-key-event-handlers, despite having read and re-read
>the docs and sample code a hundred times.  Here is a simple 
>example that fails to handle key events -- all keystrokes go 
>to the listener:
>
>(defclass simple-key-handling-view (dialog-item)
>  ()
>  )
>
>;;; When you get a keystroke, draw that character.
>;;;  ***BUT NOTHING HAPPENS
>
>(defmethod view-key-event-handler ((item simple-key-handling-view)
>char)
>  "Just print the character in the view."
>  (when (integerp char) (setq char (code-char char)))  ; (why?)
>  (move-to item 20 20)
>  (format item "~c" char))
>
>(setq *w* (make-instance 'window))
>(setq *v* (make-instance 'simple-key-handling-view
>            :view-position #@(4 4)
>            :view-size #@(200 200)))
>
>(add-subviews *w* *v*)
>
>;;; (key-handler-p *v*)  --> NIL
>
>
>Please don't tell me to use an instance of some text dialog
>item; I want to do something very special with the keystrokes,
>and anyway there must be a simpler way...
>
>Many, many thanks for your kind attention.

The problem is that the window is not passing the VIEW-KEY-EVENT-HANDLER
call on to your dialog item. If you mix in the KEY-HANDLER-MIXIN class, this
will be done for you. It also makes KEY-HANDLER-P return true.

(defclass simple-key-handling-view (key-handler-mixin dialog-item)
  ()
  )

It looks a little better if you erase before drawing. Also, the CHAR arg
to view-key-event-handler is a character. No need to coerce it from an
integer:

(defmethod view-key-event-handler ((item simple-key-handling-view) char)
  "Just print the character in the view."
;  (when (integerp char) (setq char (code-char char)))  ; (why?)
  (with-focused-view (view-container item)
    (rlet ((rect :rect :topleft #@(0 0) :botright #@(100 100)))
      (#_EraseRect rect)))
  (move-to item 20 20)
  (format item "~c" char))