Re: key snoopers



Paul Davis <pbd op net> writes:

> can someone tell me a little about a key snooper - what is it, what is
> it for ? 
> 
> i'm contemplating using it to completely subvert the default GTK+
> method of handling keystrokes, which is at odds with the way some of
> my GUI's need to work.

The key snooper is exactly what you suspect it be: you get all key
events before GTK+ processes them. Inside the snooper function you
can decide for each event if you want to let is pass through to GTK+
or if you just want to throw it away.

If you need to queue some events and decide wheter to keep them or
throw them away you can use code like this to record the events
and playback them later if desired:


static GList       *event_queue           = NULL;
static gint         playback_ignore_count = 0;

static gboolean
key_snooper (GtkWidget   *widget,
             GdkEventKey *event,
             gpointer     data)
{
  if (playback_ignore_count > 0)
    {
      playback_ignore_count--;
      return FALSE;
    }

  if (want_to_put_event_to_queue)
    {
       event_queue_add (event);
       return TRUE;
    }
  else if (want_to_let_event_through)
    {
       return FALSE;
    }
  else if (noticed_that_queued_events_need_to_go_to_gtk)
    {
       event_queue_add (event);
       event_queue_rollback ();
       return TRUE;
    }
  else if (noticed_that_event_queue_needs_to_be_cleared)
    {
       event_queue_clear ();
    }
  else if (blah)

   (...)
}

static void
event_queue_add (GdkEventKey *event)
{
  event_queue = g_list_prepend (event_queue,
                                gdk_event_copy ((GdkEvent *) event));
}

static void
event_queue_rollback (void)
{
  g_list_foreach (event_queue, (GFunc) gdk_event_put, NULL);

  playback_ignore_count = g_list_length (event_queue);

  remote_event_queue_clear ();
}

static void
event_queue_clear (void)
{
  g_list_foreach (event_queue, (GFunc) gdk_event_free, NULL);
  g_list_free (event_queue);

  event_queue = NULL;
}


ciao,
--Mitch




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