[Openmcl-devel] keyDown events

R. Matthew Emerson rme at clozure.com
Tue Nov 6 11:31:43 PST 2007

On Nov 6, 2007, at 2:09 PM, Ron Garret wrote:

> This works:
> (defclass scribble-view (ns:ns-view)
>   ()
>   (:metaclass ns:+ns-object))
> (ccl::define-objc-method ((:void :mouse-down (:id event)) scribble- 
> view)
>   (ccl::slet* ((event-location (ccl::send event 'location-In-Window))
> 	       (view-location (ccl::send self :convert-Point event-
> location :from-View nil)))
>     (with-focused-view self
>       (#/drawAtPoint:withAttributes: #@"x" view-location +null-ptr 
> +))))
> I can stick a scribble-view in a window and click on it to paint
> little x's all over.  But when I try this:
> (ccl::define-objc-method ((:void :key-down (:id event)) scribble-view)
>   [do-something...]
> it doesn't work.  I activate the window, press some keys, and as far
> as I can tell the key-down method is not invoked at all.  From
> everything that I've read this ought to work.  What am I doing wrong?

Override #/acceptsFirstResponder to return t (or #$YES), i.e.,

(objc:defmethod (#/acceptsFirstResponder :<BOOL>) ((self scribbler- 

Also, in Real Life, you want to do all your drawing in #/drawRect:.   
So, in this case, you'd have a slot (instance variable) in your  
scribble-view, where you'd keep track of mouse clicks, maybe a list of  
points or something.  All you'd do in #/mouseDown: would be to add the  
point to the list, and then say (#/setNeedsDisplay: self t).  #/ 
drawRect: would then run through the list of points, drawing them to  
the screen.

Experimenting isn't real life, of course, but I wanted to mention it.

More information about the Openmcl-devel mailing list