Re: GtkText key events

>>>>> "J" == Jeremy Wise <> writes:

 J> Hello,


 J> I need to know how to capture keys as they're pressed in a GtkText
 J> widget.  I want the keys to be captured and then sent to the
 J> normal processing routines, so I can get the pressed key, as well
 J> as make it come up in the textbox.  I also need to know when
 J> backspace, enter, arrow keys, etc are pressed.  Any ideas?

You simply need to catch the key_press event as in the program
attached.  Unless you explicitly stops the event handling, the text
widget will handle the event after you are through with it. The
different constants (for key left, home, enter etc) are found in


#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>	/* to get key constants */

static gint
key_pressed (GtkWidget *text, GdkEventKey *ev)
  g_return_val_if_fail (text != NULL, FALSE);
  g_return_val_if_fail (ev != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_TEXT (text), FALSE);

  switch (ev->keyval) {
  case GDK_Return:
    g_print ("<Enter>\n");
  case GDK_Tab:
    g_print ("<Tab>");

    /* Just for fun, if $ is pressed, we stop event processing */
  case '$':
    g_print ("\nI got a '$' but I'm not gonna insert it!\n");
    gtk_signal_emit_stop_by_name (GTK_OBJECT (text), "key_press_event");
    return TRUE;
    g_print ("%c",ev->keyval);

  return FALSE;

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

  /* initialize */
  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  text = gtk_text_new (NULL, NULL);
  gtk_text_set_editable (GTK_TEXT (text), TRUE);
  gtk_container_add (GTK_CONTAINER (window), text);
  /* setup event handler */
  gtk_signal_connect (GTK_OBJECT (text), "key_press_event",
		      GTK_SIGNAL_FUNC (key_pressed), NULL);

  /* show */
  gtk_widget_show_all (window);

  /* main loop */
  gtk_main ();
  return 0;

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