On Sat, Feb 17, 2001 at 01:36:27AM -0500, Buzz Megg wrote:
> Well, since I got such an overwhelming response to my previous query (ie. 
> *nobody*) and attaching to keypress events was brought up recently, I 
> thought that I'd try again.
> 
> >From previous query:
> >Okay, I'm trying to get key_press events from a drawing area.  My
> >solution works only for arrow keys.  And then only if they are
> >pressed twice in a row.  Any ideas what I'm doing wrong?
> 

In GTK the arrow keys move the focus to another widget.
If you add a button or something to your example you can see it happening.
That's why you have to press twice in a row, so that the focus comes back
around to toppane.

Here's one solution:

#!/usr/bin/env python
from gtk import *
import GDK

def focus_in_impl (win, event):
    win.set_flags (HAS_FOCUS)
    return TRUE

def focus_out_impl (win, event):
    win.unset_flags (HAS_FOCUS)
    return TRUE

def enter_notify_impl (win, event):
    win.grab_focus ()
    return FALSE

def button_impl(win, event):
    print event, "b =", event.button, "x =", event.x, "y =", event.y
    win.grab_focus ()
    return FALSE

def key_press_impl(win,event):
    print "Key event received:",win,event, event.string
    return TRUE

def quit(*args):
    mainquit()

topwindow = GtkWindow()

topbox = GtkVBox()
topwindow.add(topbox)                             
topbox.show()

b = GtkButton ('blah')
topbox.pack_start (b)

toppane = GtkDrawingArea()
toppane.set_usize(200,200)
topbox.pack_start (toppane)
toppane.show()

toppane.set_events(GDK.ALL_EVENTS_MASK)
toppane.set_flags(CAN_FOCUS) 
toppane.connect ("focus_in_event", focus_in_impl)
toppane.connect ("focus_out_event", focus_out_impl)
toppane.connect("button_press_event",button_impl)
toppane.connect ("enter_notify_event", enter_notify_impl)
#toppane.connect ("key_press_event",key_press_impl)
toppane.connect_after ("key_press_event",key_press_impl)

topwindow.connect("destroy",quit)   
topwindow.connect("delete_event",quit)
topwindow.show_all()

mainloop()


This "works", but I'm not sure if it's the right way of doing things.
You can't do toppan.connect("key_press...) as opposed to 
toppane.connect_after("key_press...) and you have to make sure that 
key_press_impl() returns TRUE.  I don't understand why.
I suppose the right way might be to use signal_emit_stop() or something,
but I don't know much about that either.


> Thanks,
> Andy
[snip]


_______________________________________________
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk

Reply via email to