how to handle key presses?!?



i am still trying to get key press events to work. ifound out why g_print
was only printing after the program exitted, there needs to be a '\n' in
the printed data. i suppose that flushes the output.

at any rate, i've poured over all the related example code i could find, 
the header files and the gtk code itself. i haven't found any code yet
that  explicitly shows the use of keypress events. would someone please
help me out? i have also included the source file itself.

th following is the gtk portion of the code:

 glarea = GTK_WIDGET(gtk_gl_area_new(attrlist));
  gtk_widget_set_events(GTK_WIDGET(glarea),
                        GDK_EXPOSURE_MASK|
                        GDK_KEY_PRESS_MASK|
                        GDK_BUTTON_PRESS_MASK);
  gtk_signal_connect(GTK_OBJECT(glarea), "expose_event",
                     GTK_SIGNAL_FUNC(draw), NULL);
  gtk_signal_connect(GTK_OBJECT(glarea), "configure_event",
                     GTK_SIGNAL_FUNC(reshape), NULL);
  gtk_signal_connect(GTK_OBJECT(glarea), "realize",
                     GTK_SIGNAL_FUNC(init), NULL);
  
  gtk_signal_connect(GTK_OBJECT(glarea), "button_press_event",
                        GTK_SIGNAL_FUNC(Button), NULL);
  
  gtk_signal_connect(GTK_OBJECT(glarea), "key_press_event",
                        GTK_SIGNAL_FUNC(ProcessInput), NULL);
  gtk_widget_set_usize(glarea, 400,400);
  window = gtk_window_new( GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "glmod");
  gtk_container_border_width(GTK_CONTAINER(window), 10);
  
  gtk_signal_connect(GTK_OBJECT(window), "delete_event",
                     GTK_SIGNAL_FUNC(glmodQuit), NULL);
  
  gtk_quit_add_destroy(1, GTK_OBJECT(window));
                     
  gtk_container_add(GTK_CONTAINER(window),GTK_WIDGET(glarea));
  gtk_widget_show(GTK_WIDGET(glarea));
  gtk_widget_show(GTK_WIDGET(window));



"ttfn, ta-ta for now!"-- t-i-double 'g'-er

http://www.op.net/~finklesk/index.html



                                   " STUFF. "


#include <gtk/gtk.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "gtkglarea.h"
#include "glmod.h"

gint init(GtkWidget *widget)
{
  /* OpenGL functions can be called only if begingl returns true */
  if (gtk_gl_area_begingl(GTK_GL_AREA(widget))) {
	PreSceneSetup();
    SetViewPort( widget->allocation.width, widget->allocation.height);

/*    glViewport(0,0, widget->allocation.width,
widget->allocation.height);*/
    /* end opengl calls by calling endgl */
    gtk_gl_area_endgl(GTK_GL_AREA(widget));
  }
  return TRUE;
}


/* When widget is exposed it's contents are redrawn. */
gint draw(GtkWidget *widget, GdkEventExpose *event)
{
  /* Draw only last expose. */
  if (event->count > 0) {
    return TRUE;
  }

  /* OpenGL functions can be called only if gtk_gl_area_begingl
     returns true, you can't call gl* functions anywhere except
     inside gtk_gl_area_begingl()/gtk_gl_area_endgl() pairs.
  */
  if (gtk_gl_area_begingl(GTK_GL_AREA(widget))) {

	DrawFrame();
   /* Opengl rendering is done for now. */
    gtk_gl_area_endgl(GTK_GL_AREA(widget));
  }

  /* Swap backbuffer to front */
  gtk_gl_area_swapbuffers(GTK_GL_AREA(widget));
  
  return TRUE;
}

/* When glarea widget size changes, viewport size is set to match the new size */
gint reshape(GtkWidget *widget, GdkEventConfigure *event)
{
  /* OpenGL functions can be called only if begingl returns true */
  if (gtk_gl_area_begingl(GTK_GL_AREA(widget))) {

    SetViewPort( widget->allocation.width, widget->allocation.height);
	DrawFrame();
    /* end opengl calls by calling endgl */
    gtk_gl_area_endgl(GTK_GL_AREA(widget));
  }
  return TRUE;
}

gint Button(GtkWidget *wid, GdkEventButton *event)
	{
	g_print("button pressed\n");
	return TRUE;
	}

gint ProcessInput(GtkWidget *wid, GdkEventKey *keypress)
	{
/*	char chr = (guint *) keypress->keyval;
	
	ProcessKey( chr);
	
	if(gtk_gl_area_begingl(GTK_GL_AREA(wid)))
		{
		DrawFrame();
		gtk_gl_area_endgl(GTK_GL_AREA(wid));
		} */
	g_print("%s\n", keypress->keyval);
	g_print("where am i?");
	return TRUE;
	}


void glmodQuit(GtkWidget *wid, gchar *dat)
	{
	PostSceneCleanup();
	gtk_main_quit();
	}


int main(int argc, char **argv)
{
  GtkWidget *window,*glarea;

  /* Attribute list for gtkglarea widget. Specifies a
     list of Boolean attributes and enum/integer
     attribute/value pairs. The last attribute must be
     GDK_GL_NONE. See glXChooseVisual manpage for further
     explanation.
  */
  gint attrlist[] = {
    GDK_GL_RGBA,
    GDK_GL_DOUBLEBUFFER,
    GDK_GL_DEPTH_SIZE,1,
    GDK_GL_NONE
  };

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

  /* Check if OpenGL (GLX extension) is supported. */
  if (gdk_gl_query() == FALSE) {
    g_print("OpenGL not supported\n");
    return 0;
  }

  /* Create new OpenGL widget. */
  glarea = GTK_WIDGET(gtk_gl_area_new(attrlist));
  /* Events for widget must be set before X Window is created */
  gtk_widget_set_events(GTK_WIDGET(glarea),
			GDK_EXPOSURE_MASK|
			GDK_KEY_PRESS_MASK|
			GDK_BUTTON_PRESS_MASK);

  /* Connect signal handlers */
  /* Redraw image when exposed. */
  gtk_signal_connect(GTK_OBJECT(glarea), "expose_event",
		     GTK_SIGNAL_FUNC(draw), NULL);
  /* When window is resized viewport needs to be resized also. */
  gtk_signal_connect(GTK_OBJECT(glarea), "configure_event",
		     GTK_SIGNAL_FUNC(reshape), NULL);
  /* Do initialization when widget has been realized. */
  gtk_signal_connect(GTK_OBJECT(glarea), "realize",
		     GTK_SIGNAL_FUNC(init), NULL);

  gtk_signal_connect(GTK_OBJECT(glarea), "button_press_event",
			GTK_SIGNAL_FUNC(Button), NULL);

/*  gtk_signal_connect(GTK_OBJECT(glarea), "key_press_event",
			GTK_SIGNAL_FUNC(ProcessInput), NULL);
*/

  /* set minimum size */
  gtk_widget_set_usize(glarea, 400,400);

  /* Create new top level window. */
  window = gtk_window_new( GTK_WINDOW_TOPLEVEL);    
  gtk_window_set_title(GTK_WINDOW(window), "glmod");
  gtk_container_border_width(GTK_CONTAINER(window), 10);
  
  /* Quit form main if got delete event */
  gtk_signal_connect(GTK_OBJECT(window), "delete_event",
                     GTK_SIGNAL_FUNC(glmodQuit), NULL);
                        
  /* You should always delete gtk_gl_area widgets before exit or else
     GLX contexts are left undeleted, this may cause problems (=core dump)
     in some systems.
     Destroy method of objects is not automatically called on exit.
     You need to manually enable this feature. Do gtk_quit_add_destroy()
     for all your top level windows unless you are certain that they get
     destroy signal by other means.   
  */
  gtk_quit_add_destroy(1, GTK_OBJECT(window));
  
		    
  /* put glarea into window and show it all */
  gtk_container_add(GTK_CONTAINER(window),GTK_WIDGET(glarea));
  gtk_widget_show(GTK_WIDGET(glarea));
  gtk_widget_show(GTK_WIDGET(window));

  gtk_main();

  return 0;
}


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