updating labels when a key is stroked
- From: Eric Wajnberg <eric wajnberg sophia inra fr>
- To: gtk-app-devel-list gnome org
- Subject: updating labels when a key is stroked
- Date: Wed, 07 Aug 2013 14:48:37 +0200
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.
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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]