Re: [gtk-list] Intercepting keyboard input




Robert Roebling <roebling@ruf.uni-freiburg.de> writes:

> is it possible to intercept keyboard input to
> a text widget, e.g. in order to only allow
> input of alphanumeric characters? I am not
> so much interested in that specific problem,
> more in the general way. As far as I understood
> can the signal-connect system only be used,
> to get notified, when something happens. There is
> no way (apparent to me) to actually change
> what is going on (as described above e.g.).
> Am I right?

Try something like:

gint
key_press_event (GtkWidget *w, GdkEventKey *event)
{
 /* You really should look at the characters in event->string instead */
 if (!((event->keyval >= 0x20) && (event->keyval <= 0xFF) && 
     isalnum(event->keyval)))
   {
     /* Stop the current processing of this signal */
     gtk_signal_emit_stop_by_name (GTK_OBJECT (w), "key_press_event");
     /* But return "not handled", so accelerators, etc. are checked */
     return FALSE;  
   }
}

(Not tested, but the general idea should be correct)

This won't work for every signal - if the signal was created with
GTK_RUN_FIRST, then the default signal handler will be run _before_
your handler, so by the time your handler gets the signal, it's
too late.

Regards,
                                        Owen



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