Re: Bus failure using focus events



On Thursday 02 October 2003 19:35, Ian Schorr wrote:
> Can someone maybe point me to example code implementing
> "focus-out-event", particularly against a Text Entry widget?  That
> might help me even more.  I've seen several references to it being used
> this way, but I haven't found a good example (and the GTK docs need to
> be taken out and shot).

This isn't exactly what you asked for, but I found a program with a 
"focus_in_event" and a "focus_out_event".  It is slightly modified code from 
Chapter 12 of "Gnome/GTK+ Programming Bible" - see the book for a 
description.  Run two copies of the program to see how they handle the 
exchange of focus.  Any keyboard activity will be reported by the display 
with the focus. - Good Luck

Dave Smith
/** keylisten.c **/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

gint eventDelete(GtkWidget *widget,GdkEvent *event,gpointer data);
gint eventDestroy(GtkWidget *widget,GdkEvent *event,gpointer data);
gint eventFocus(GtkWidget *widget,GdkEvent *event,gpointer data);
gint eventPressKey(GtkWidget *widget,GdkEvent *event,gpointer data);
gint eventReleaseKey(GtkWidget *widget,GdkEvent *event,gpointer data);

static GtkWidget *keyLabel, *scrollLabel, *focusLabel;

gboolean altKeyDown = FALSE;
gboolean controlKeyDown = FALSE;
gboolean metaKeyDown = FALSE;
gboolean leftShiftKeyDown = FALSE;
gboolean rightShiftKeyDown = FALSE;

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

  gtk_init(&argc,&argv);
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window),"keylisten");
  gtk_widget_set_size_request(window,600,160);
  gtk_container_set_border_width(GTK_CONTAINER(window),30);
  gtk_signal_connect(GTK_OBJECT(window),"delete_event",
    GTK_SIGNAL_FUNC(eventDelete),NULL);
  gtk_signal_connect(GTK_OBJECT(window),"destroy",
    GTK_SIGNAL_FUNC(eventDestroy),NULL);
  gtk_signal_connect(GTK_OBJECT(window),"focus_in_event",
    GTK_SIGNAL_FUNC(eventFocus),NULL);
  gtk_signal_connect(GTK_OBJECT(window),"focus_out_event",
    GTK_SIGNAL_FUNC(eventFocus),NULL);
  gtk_signal_connect(GTK_OBJECT(window),"key_press_event",
    GTK_SIGNAL_FUNC(eventPressKey),NULL);
  gtk_signal_connect(GTK_OBJECT(window),"key_release_event",
    GTK_SIGNAL_FUNC(eventReleaseKey),NULL);

  box = gtk_vbox_new(FALSE,0);

  keyLabel = gtk_label_new(" ");
  gtk_box_pack_start(GTK_BOX(box),keyLabel,FALSE,FALSE,0);

  scrollLabel = gtk_label_new(" ");
  gtk_box_pack_start(GTK_BOX(box),scrollLabel,FALSE,FALSE,0);

  focusLabel = gtk_label_new("Focus?");
  gtk_box_pack_end(GTK_BOX(box),focusLabel,FALSE,FALSE,0);

  gtk_container_add(GTK_CONTAINER(window),box);
  gtk_widget_show_all(window);
  gtk_main();
  exit(0);
}
gint eventFocus(GtkWidget *widget,GdkEvent *event,gpointer data) {
  GdkEventFocus *focus;
  if(event->type == GDK_FOCUS_CHANGE) {
    focus = (GdkEventFocus *)event;
    if(focus->in) gtk_label_set_text(GTK_LABEL(focusLabel),"Focus acquired");
    else gtk_label_set_text(GTK_LABEL(focusLabel),"Focus lost");
    }
    return(TRUE);
}
gint eventPressKey(GtkWidget *widget,GdkEvent *event,gpointer data) {
  static char keyList[80] = " ";
  char lastKey[80];
  int i, shift;
  char work[10];
  GdkEventKey *key = (GdkEventKey *)event;

  switch(key->keyval) {
  case GDK_Alt_L:
  case GDK_Alt_R:
    altKeyDown = TRUE;
    break;
  case GDK_Control_L:
  case GDK_Control_R:
    controlKeyDown = TRUE;
    break;
  case GDK_Meta_L:
  case GDK_Meta_R:
    metaKeyDown = TRUE;
    break;
  case GDK_Shift_L:
    leftShiftKeyDown = TRUE;
    break;
  case GDK_Shift_R:
    rightShiftKeyDown = TRUE;
    break;
  }

  strcpy(work,gdk_keyval_name(key->keyval));
  strcat(keyList," ");
  strcat(keyList,work);
  shift = strlen(keyList) - 60;
  if(shift > 0)
    for(i=shift; keyList[i-1] != '\0'; i++) keyList[i-shift] = keyList[i];
  gtk_label_set_text(GTK_LABEL(scrollLabel),keyList);

  sprintf(lastKey,"GDK_%s",work);
  if((key->keyval >= 0x20) && (key->keyval < 0x7F)) {
    sprintf(work,"    %c",(gchar)key->keyval);
    strcat(lastKey,work);
  }
  if(gdk_keyval_is_lower(key->keyval)) strcat(lastKey,"   (not upper case)");
  else if(gdk_keyval_is_upper(key->keyval)) strcat(lastKey,"   (upper case)");
  sprintf(work,"     [%02X]",key->keyval);
  strcat(lastKey,work);
  if(altKeyDown) strcat(lastKey,"  ALT");
  if(controlKeyDown) strcat(lastKey,"  CONTROL");
  if(metaKeyDown) strcat(lastKey,"  META");
  if(leftShiftKeyDown) strcat(lastKey,"  LeftSHIFT");
  if(rightShiftKeyDown) strcat(lastKey,"  RightSHIFT");
  gtk_label_set_text(GTK_LABEL(keyLabel),lastKey);

  return(TRUE);
}
gint eventReleaseKey(GtkWidget *widget,GdkEvent *event,gpointer data) {
  GdkEventKey *key = (GdkEventKey *)event;

  switch(key->keyval) {
  case GDK_Alt_L:
  case GDK_Alt_R:
    altKeyDown = FALSE;
    break;
  case GDK_Control_L:
  case GDK_Control_R:
    controlKeyDown = FALSE;
    break;
  case GDK_Meta_L:
  case GDK_Meta_R:
    metaKeyDown = FALSE;
    break;
  case GDK_Shift_L:
    leftShiftKeyDown = FALSE;
    break;
  case GDK_Shift_R:
    rightShiftKeyDown = FALSE;
    break;
  }
  return(TRUE);
}

gint eventDelete(GtkWidget *widget,GdkEvent *event,gpointer data) {
  return(FALSE);
}

gint eventDestroy(GtkWidget *widget,GdkEvent *event,gpointer data) {
  gtk_main_quit();
  return(0);
}


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