Re: keyboard input

On Mon, 2011-02-14 at 10:00 -0500, Paul Davis wrote:
> On Sun, Feb 13, 2011 at 8:25 PM, Maximilian Schneider
> <max schneidersoft net> wrote:
> > Hello!
> >
> > I'm working on a CAD like program using input elements similar to
> > inkscape; clicking the rectangle button or pushing the 'r' key.
> >
> > Whats the best way to implement something like this in C? I've taken a
> > look at the inkscape source, but it being in C++, I can't make much use
> > of it.
> add a key press/release event handler for the window.
>     switch (ev->keyval) {
>     case GDK_KEY_foo:
>           do_something ();
>           break;
>     ....
> be careful about deciding whether you want to drive actions on press or release.
> for better overall app design, make all "do_something()" methods into
> GtkActions, and then
>     switch (ev->keyval) {
>     case GDK_KEY_foo:
>             action = get_action ("do_something");
>             action->activate ();
> (the implementation of "get_action" is left as an exercise for the reader)
> this then allows you to set up GUI elements like buttons as proxies
> for the action, and then your entire GUI is nice and consistent.

Thanks allot for your reply.

GtkAction seems to be exactly what I was looking for to consolidate all
the different ways a single UI functionality could be invoked.

Taking a step back; so far I have been capturing mouse events, move,
press, release, and scroll, using something like this:

struct mouse_callbacks cbl;	//callback list

static gint button_press_event( GtkWidget *, GdkEventButton * );
static gint motion_notify_event( GtkWidget *, GdkEventMotion * );
static gint button_release_event( GtkWidget *, GdkEventButton * );
static gint scroll_event( GtkWidget *, GdkEventScroll * );

call this to set up handlers for mouse events.
void mouse_attach_handlers( GtkWidget *win, struct mouse_callbacks *s ){
	gtk_widget_add_events( win, GDK_BUTTON_PRESS_MASK |

	g_signal_connect( GTK_OBJECT(win), "motion_notify_event",
			G_CALLBACK(motion_notify_event), NULL );
	g_signal_connect( GTK_OBJECT(win), "button_press_event",
			G_CALLBACK(button_press_event), NULL );
	g_signal_connect( GTK_OBJECT(win), "button_release_event",
			G_CALLBACK(button_release_event), NULL );
	g_signal_connect( GTK_OBJECT(win), "scroll_event",
			G_CALLBACK(scroll_event), NULL );

	cbl = *s;

/* I'll only show the scroll event to save space.
   but the idea is that these functions handle all the dirty work like
keeping track of dragging etc.
static gint scroll_event( GtkWidget *widget, GdkEventScroll *event ){
	int dir = 0;

	if(event->direction == GDK_SCROLL_UP){
		dir = 1;
	}else if(event->direction == GDK_SCROLL_DOWN){
		dir = 0;

		cbl.scroll_func( event->x, event->y, dir, cbl.scroll_arg );


The problem I'm facing is that when adding keyboard events, mouse events
are no longer registered.

	gtk_widget_add_events( win, GDK_KEY_PRESS_MASK |

	g_signal_connect( GTK_OBJECT(win), "key_press_event",
			G_CALLBACK(key_press_event), NULL );
	g_signal_connect( GTK_OBJECT(win), "key_release_event",
			G_CALLBACK(key_release_event), NULL );

in this case I am trying to attach the mouse events to a drawing area
and the keyboard events to the main window.

Im also not seeing how a GtkActions activate function actually does
I'm assuming its similar to events and should be listened for in the
appropriate places?

Max. S.

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