c - how to program window to close with escape key -


i've been searching on place find out how program close using escape key. not many people use gtk+ assume. also, if it's not asking much, can please show me how use gnome developer website find stuff out on own? thanks. so, closing window escape key, here's code:

#include <stdio.h> #include <string.h> #include <gtk/gtk.h> #include <stdlib.h> #include <ctype.h> #include <curses.h>  #define key_esc '\033'  static gtkwidget *entry;  static gboolean kill_window(gtkwidget *widget, gdkevent *event, gpointer data) {     gtk_main_quit();     return false; }  static void button_press(gtkwidget *widget, gpointer data) {     const char *text = gtk_entry_get_text(gtk_entry(entry));     //system("cd" text);     //printf("%s\n", text);     const char *text2 = "&";     char *concatenation;     concatenation = malloc(strlen(text)+2);     strcpy(concatenation, text);     strcat(concatenation, text2);     system(concatenation);      gtk_main_quit(); }      int main(int argc, char *argv[])     {  gtkwidget *window; gtkwidget *button; gtkwidget *button1; gtkwidget *hbox;   gtk_init(&argc, &argv);  window = gtk_window_new(gtk_window_toplevel); button = gtk_button_new_with_label("run"); button1 = gtk_button_new_with_label("cancel"); entry = gtk_entry_new(); hbox = gtk_vbox_new(false, 2);  gtk_window_set_title(gtk_window(window), "run..");  gtk_window_set_position(gtk_window(window), gtk_win_pos_center_always); g_signal_connect(window, "delete_event", g_callback(kill_window), null);  g_signal_connect(button, "clicked", g_callback(button_press), null);  g_signal_connect(button1, "clicked", g_callback(kill_window), null); g_signal_connect(entry, "activate", g_callback(button_press), null); g_signal_connect(button1, "delete_event", g_callback(kill_window), null); gtk_window_set_resizable(gtk_window(window), false); gtk_window_set_default_size(gtk_window(window), 250, 100); gtk_window_set_decorated(gtk_window(window), true);  gtk_box_pack_start(gtk_box(hbox), entry, true, true, 2);  gtk_box_pack_start(gtk_box(hbox), button, false, false, 2);  gtk_box_pack_start(gtk_box(hbox), button1, false, false, 2); gtk_container_add(gtk_container(window), hbox); gtk_widget_show_all(window);  gtk_main();   return 0; } 

connect key_press_event signal on top-level window:

g_signal_connect(window, "key_press_event", g_callback(check_escape), null); 

the check_escape callback this:

#include <gdk/gdkkeysyms.h> ... static gboolean check_escape(gtkwidget *widget, gdkeventkey *event, gpointer data) {   if (event->keyval == gdk_key_escape) {     gtk_main_quit();     return true;   }   return false; } 

also, don't need <curses.h> include, nor key_esc definition.

if top-level window dialog, might consider using gtkdialog instead of gtkwindow. dialog provides stock responses such "ok" , "cancel", enter , escape keys invoking expected response.


Comments