Skip to content

Instantly share code, notes, and snippets.

@makotokato
Created October 10, 2020 04:35
Show Gist options
  • Save makotokato/ca97bb7467deeef7a8a274623709b4d6 to your computer and use it in GitHub Desktop.
Save makotokato/ca97bb7467deeef7a8a274623709b4d6 to your computer and use it in GitHub Desktop.
GTK3 keyboard sample
// build:
// gcc gtk3-entry-keyboard-event.c `pkg-config --cflags gtk+-3.0` `pkg-config
// --libs gtk+-3.0`
#include <gtk/gtk.h>
static gboolean print_keypress(GtkWidget *widget, GdkEventKey *event,
gpointer user_data) {
printf("keypress: %#06x %#06x\n", event->keyval, event->hardware_keycode);
return FALSE;
}
static gboolean print_keyrelease(GtkWidget *widget, GdkEventKey *event,
gpointer user_data) {
printf("keyrelesae: %#06x %#06x\n", event->keyval, event->hardware_keycode);
return FALSE;
}
static void activate(GtkApplication *app, gpointer user_data) {
GtkWidget *window;
GtkWidget *entry;
GtkWidget *grid;
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "Window");
gtk_widget_add_events(window, GDK_KEY_PRESS_MASK);
g_signal_connect(G_OBJECT(window), "key_press_event",
G_CALLBACK(print_keypress), "window");
g_signal_connect(G_OBJECT(window), "key_release_event",
G_CALLBACK(print_keyrelease), "window");
grid = gtk_grid_new();
gtk_container_add(GTK_CONTAINER(window), grid);
entry = gtk_entry_new();
gtk_grid_attach(GTK_GRID(grid), entry, 0, 0, 1, 1);
gtk_widget_show_all(window);
}
int main(int argc, char **argv) {
GtkApplication *app;
int status;
app = gtk_application_new("org.example.keyboard", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment