Re: [gtk-list] Re: keypress events and DrawingArea



Alistair Cunningham wrote:
> 
> On Thu, 28 May 1998, Janne Lof wrote:
> 
> >
> >Hi,
> >
> >I am trying to capture keypress events for DrawingArea widget, but for
> >some reason my keypress handler never gets called.
> >
> >Is following right way to do it, or am I forgetting something?
> >
> >  widget = gtk_drawing_area_new();
> >  gtk_widget_set_events(widget,
> >                       GDK_EXPOSURE_MASK|
> >                       GDK_BUTTON_PRESS_MASK|
> >                       GDK_KEY_PRESS_MASK);
> >
> >  gtk_signal_connect(GTK_OBJECT(widget), "key_press_event",
> >                    GTK_SIGNAL_FUNC(keypress), NULL);
> 
> Are you packing the drawing area into a scrolled window, or something
> similar? If so, they get the keypresses, not the drawing area. For a
> program I'm working on, I ended up with:
> 
> A drawing area
> inside a scrolled widow
> inside a event box
> inside a top level window.
> 
> Then worked out whether the keypresses to the event box were for the
> drawing area or scroll bars. Not nice.
> 

The following code works with gtk+-0.99.9 at college...
#include <gtk/gtk.h>

static gint
key_press_event (GtkWidget *widget, GdkEventKey *event)
{
  g_print ("%d\n", event->keyval);
  return TRUE;
}

int
main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *drawing_area;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  drawing_area = gtk_drawing_area_new ();
  gtk_container_add (GTK_CONTAINER (window), drawing_area);
  gtk_drawing_area_size (GTK_DRAWING_AREA (drawing_area), 200, 200);
  gtk_widget_set_events (drawing_area,
                         GDK_EXPOSURE_MASK
                         | GDK_KEY_PRESS_MASK);
  gtk_signal_connect (GTK_OBJECT (drawing_area), "key_press_event",
                      GTK_SIGNAL_FUNC (key_press_event), NULL);
  gtk_widget_show (drawing_area);
  gtk_widget_show (window);

  gtk_main ();
  return TRUE;
}

BUT it doesnt't work with gtk+-1.0.3 which I have at home and I really
cannot figure out why. I am certainly no expert but I suppose that
something must have changed between these vesrions which we didn't
notice. Perhaps someone knows...
-- 
--------------------------------------
http://www-students.doc.ic.ac.uk/~gz4/
--------------------------------------



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