Re: updating labels when a key is stroked
- From: Eric Wajnberg <eric wajnberg sophia inra fr>
- To: gtk-app-devel-list gnome org
- Subject: Re: updating labels when a key is stroked
- Date: Fri, 09 Aug 2013 12:14:14 +0200
Ok, thanks!
I'm just plain stupid. I indeed just have to collect a "key_press_event"
event and that's it!
Sorry for disturbing the list with such a basic question (I am still
learning how to use gtk)..
Cheers to all,
Eric.
Kang Hu wrote, On 09/08/2013 09:39,
all you need is an event filter.
remove the 'g_timeout_add' line with the following code.
0. get the gdk window of the created top-level gtk window.
GdkWindow
<https://developer.gnome.org/gdk2/stable/gdk3-Windows.html#GdkWindow>*
gtk_widget_get_window
<https://developer.gnome.org/gtk3/3.8/GtkWidget.html#gtk-widget-get-window>(/|GtkWidget
<https://developer.gnome.org/gtk3/3.8/GtkWidget.html> *widget|/);
GdkWindow* gdkwindow = gtk_widget_get_window (pWindow);
1. add event mask you're interested in
voidgdk_window_set_events
<https://developer.gnome.org/gdk3/3.8/gdk3-Windows.html#gdk-window-set-events>(/|GdkWindow
<https://developer.gnome.org/gdk3/3.8/gdk3-Windows.html#GdkWindow>
*window|/,/|GdkEventMask
<https://developer.gnome.org/gdk3/3.8/gdk3-Events.html#GdkEventMask>
event_mask|/);
gdk_window_set_events (gdkwindow, GDK_KEY_PRESS_MASK);
2. register an event filter
voidgdk_window_add_filter (/|GdkWindow
<https://developer.gnome.org/gdk3/3.8/gdk3-Windows.html#GdkWindow>
*window|/,/|GdkFilterFunc
<https://developer.gnome.org/gdk3/3.8/gdk3-Windows.html#GdkFilterFunc>
function|/,/|gpointer
<https://developer.gnome.org/glib/unstable/glib-Basic-Types.html#gpointer>
data|/);
gdk_window_add_filter (gdkwindow, key_press_filter, pWindow);
3. write the filter function.
GdkFilterReturn
<https://developer.gnome.org/gdk3/3.8/gdk3-Windows.html#GdkFilterReturn> (*GdkFilterFunc)
(/|GdkXEvent
<https://developer.gnome.org/gdk3/3.8/gdk3-Windows.html#GdkXEvent> *xevent|/,/|GdkEvent
<https://developer.gnome.org/gdk3/3.8/gdk3-Event-Structures.html#GdkEvent> *event|/,/|gpointer
<https://developer.gnome.org/glib/unstable/glib-Basic-Types.html#gpointer> data|/);
GdkFilterReturn key_press_filter (GdkXEvent* xevent, GdkEvent*
event, gpointer data)
{
// how to process key press events.
}
On Wed, Aug 7, 2013 at 8:48 PM, Eric Wajnberg
<eric wajnberg sophia inra fr <mailto:eric wajnberg sophia inra fr>>
wrote:
Hi there,
I need to develop a gtk code in which a label is changed each time
a key is stroked on the keyboad. I thus use g_timeout_add() to
launch on a regular basis a function that listens to key strokes.
Within this function, I used a combination of kbhit()/getch() to
collected the stroken keys (because I do not want the user to hit
the <return> key all the time) and update the label accordingly.
The following code is just a try, and seems ok:
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <gtk/gtk.h>
int key=0;
char string[500];
int main(int argc, char **argv)
{
/* declaration of widgets */
GtkWidget *pWindow; /*main windows */
GtkWidget *pLabel; /* a label */
void OnDestroy(GtkWidget *pWidget, gpointer pData); /*
function call back destroy */
gboolean update(gpointer pData); /* function called at regular
intervals */
/* Initialisation of GTK+ */
gtk_init(&argc, &argv);
/* creation of the main window */
pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
/* parameters of the window */
gtk_window_set_position(GTK_WINDOW(pWindow), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(pWindow), 300, 100);
gtk_window_set_title(GTK_WINDOW(pWindow), "testing key input");
/* creation of the label */
(void)sprintf(string,"%d",key);
pLabel=gtk_label_new(string);
/* adding the label to the window */
gtk_container_add(GTK_CONTAINER(pWindow), pLabel);
/* signal connexion */
g_signal_connect(G_OBJECT(pWindow), "destroy",
G_CALLBACK(OnDestroy), NULL);
/* function called at regular intervals .. */
g_timeout_add((guint)1, update,(gpointer *)pLabel);
/* showing the window */
gtk_widget_show_all(pWindow);
/* starting the loop */
gtk_main();
return EXIT_SUCCESS;
}
gboolean update(gpointer pData)
{
/* updating the label */
if (kbhit())
{
key=getch();
(void)sprintf(string, "%d",key);
gtk_label_set_label(GTK_LABEL(pData), string);
}
return TRUE;
}
void OnDestroy(GtkWidget *pWidget, gpointer pData)
{
/* stopping the loop */
gtk_main_quit();
}
The point is that this is actually not working correctly, because
the keystrokes are collected on a console that is not there! In
other words, the code above works well in "debug mode" (e.g., on
Code::Blocks) when a debugging console is available, but not in
"release mode" running without a console.
My question is thus: is there a way to listen to key stroked on
the keyboard within a gtk application?
Thanks for any help on this!
Cheers, Eric.
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org <mailto:gtk-app-devel-list gnome org>
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Eric Wajnberg
Associated Professor at the
University of Montreal (Quebec, Canada)
I.N.R.A.
400 Route des Chappes, BP 167,
06903 Sophia Antipolis Cedex, France
Tel: (33-0) 4.92.38.64.47
Fax: (33-0) 4.92.38.65.57
e-mail: wajnberg sophia inra fr
Web page: http://www.sophia.inra.fr/perso/wajnberg/
Editor-in-Chief of BioControl, Published by Springer.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]