How to capture key-press-event on gtk:event-box?

Kevin L <[email protected]> Wed, 27 Sep 2006 10:49:08 -0500
Newsgroups gmane.lisp.clg.devel
Message-ID <[email protected]>
I've got an app that creates multiple windows, each window having an 
event-box containing a drawing-area.  I am having trouble capturing key 
press/release events from the event-box.  Here is the relevant code that 
sets up a window:

---snip---

(defun create-system-window (title &key (width 400) (height 400))
   "Create a new window to contain a system"

   (let (gtk-window gtk-drawing-area gtk-event-box)
     (define-toplevel create-system-gdk-window (window title)
       (let (drawing-area event-box)

	(setf gtk-window
	      (make-instance
	       'gtk:v-box
	       :parent window
	       :child
	       (setf event-box
		     (make-instance
		      'gtk:event-box
		      :events '(:key-press :key-release :enter-notify :leave-notify)
		      :child
		      (setf drawing-area
			    (make-instance
			     'gtk:drawing-area
			     :events '(:exposure :button-press :button-release :button-motion)
			     :width-request 500 :height-request 300))))))

	;; Setup the system/model area signals
	(gtk:signal-connect drawing-area 'gdk:expose-event #'system-expose 
:object t)
	(gtk:signal-connect drawing-area 'gdk:button-press-event 
#'system-button-press)
	(gtk:signal-connect drawing-area 'gdk:button-release-event 
#'system-button-release)
	(gtk:signal-connect drawing-area 'gdk:motion-notify-event 
#'system-motion-notify)
	(gtk:signal-connect drawing-area 'gdk:scroll-event #'system-mousewheel)
	(gtk:signal-connect event-box 'gdk:enter-notify-event #'system-enter)
	(gtk:signal-connect event-box 'gdk:leave-notify-event #'system-leave)
	(gtk:signal-connect event-box 'gdk:key-press-event #'system-key-press)
	(gtk:signal-connect event-box 'gdk:key-release-event #'system-key-release)
	(setf gtk-drawing-area drawing-area)
	(setf gtk-event-box event-box)

	))

     (create-system-gdk-window)

     ;; Create the system object and tie it all together
     (make-instance 's-system
		   :name title
		   :drawing-area-window (gtk:widget-window gtk-drawing-area)
		   :window (gtk:widget-window gtk-window)
		   :event-box gtk-event-box
		   :event-box-window (gtk:widget-window gtk-event-box))))

---snip---

Two examples of event handlers:

---snip---

(defun system-motion-notify (event)
   "Handle mouse movement on the system/model window"
   (let* ((window (gdk:event-window event))
	 (system (get-system window))
	 (system-name (slot-value system 'chenl::name))
	 (x (gdk:event-x event))
	 (y (gdk:event-y event))
	 (buttons ""))

     (if *button1*
	(setf buttons "LEFT "))
     (if *button2*
	(setf buttons (concatenate 'string buttons "MIDDLE ")))
     (if *button3*
	(setf buttons (concatenate 'string buttons "RIGHT ")))

     (log-debug2 "Move to (~a, ~a)" x y)

     t))

(defun system-enter (event)
   "Notify when mouse is over system/model window"
   (let* ((window (gdk:event-window event))
	 (system (get-system-from-event-box-window window))
	 (system-name (slot-value system 'chenl::name))
	 (x (gdk:event-x event))
	 (y (gdk:event-y event)))

     (log-debug2 "Enter: ~a ~a ~a" system-name x y)

     t))

---snip---

Everything works except that when I have a window focused and hit a key, 
nothing happens.

I have tried tying the keyboard events to the drawing-area, but same 
problem.

I captured enter-notify-event and leave-notify-event specifically so I 
had a place in code to set keyboard focus on the event-box, but I'm not 
finding any functions to do that.  I can't set focus on the window or on 
  the event-box.

I feel like maybe two lines of code in #'system-enter will fix this, but 
I've no idea what they are.

I am using a relatively recent CVS snapshot.

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV