This the correct way to catch keys pressed ?



Hello all,
I'm developing an application that catch the key press and do something
depending wich key is this, my application will occupy all screen and 
the directional keys will change pixmaps in main window to select the
function to do  
Reading the helps & docs I found the gdkkeysyms.h that give the codes 
that keyval in GdkEventKey struct has, one way is compare each keyval
with values #defined in gdkkeysyms.h and execute the appropriate function,
my doubt is : this is the correct way to do this ?
Theres something in GDK/GTK that do this in a more direct way (elegant ??) 
or is this the correct way ?

Bellow is a fragment of callback of my main window keypress event signal. 

Thanks for all help and excuse-me for my bad english

Flavio Alberto



#include <gdk/gdkkeysyms.h>

gboolean
on_window_granmmi_key_press_event      (GtkWidget       *widget,
                                        GdkEventKey     *event,
                                        gpointer         user_data)
{
  enum mov_op *op;
  enum type_key *key;
  int keycatch;
  ScreenStruct *Screen;

  Screen = (ScreenStruct *) Screen_main;

  op = &Screen->key_pressed;
  key = &Screen->key_type;

  Screen->wid_father = window_granmmi;

  gtk_widget_grab_focus(GTK_WIDGET(window_base));

  keycatch = catch_key_pressed(event);

  if(keycatch < 0)
    {
      *op = UNKNOWN;
      g_print("ERROR READING KEYBOARD\n");
    }
  else
    switch(keycatch)
      {
      case GDK_Left:
	*key = DIRECTION;
	*op = LEFT;
	break;
      case GDK_Up:
	*key = DIRECTION;
	*op = UP;
	break;
      case GDK_Right:
	*key = DIRECTION;
	*op = RIGHT;
	break;
      case GDK_Down:
	*key = DIRECTION;
	*op = DOWN;
	break;
      default:
	*key = NONE;
	*op = UNKNOWN;
	g_print("UNKNOWN KEY\n");
      }
  
  execute_key_operation(Screen);
 
  return FALSE;
}


int
catch_key_pressed(GdkEventKey *keypressed)
{
  switch(keypressed->keyval)
    {
    case GDK_Left:
      return GDK_Left;
    case GDK_Up:
      return GDK_Up;
    case GDK_Right:
      return GDK_Right;
    case GDK_Down:
      return GDK_Down;
    default:
      return GDK_VoidSymbol;
    }

  return -1;
}





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