From 3ac35677a51f111c00a12850e9b9fc24f979b291 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sat, 20 Jul 2024 22:07:47 -0400 Subject: [PATCH] GTK4: Now able to capture keypresses!! --- plugins/style/style.c | 2 +- plugins/style/style.h | 2 +- rosenrot4_greenfield.c | 49 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/plugins/style/style.c b/plugins/style/style.c index 90f4d18..adc2c7c 100644 --- a/plugins/style/style.c +++ b/plugins/style/style.c @@ -1,7 +1,7 @@ #include #include #include -#define STYLE_N 8048 + 1000 +#define STYLE_N 8049 + 1000 void read_style_js(char* string) { diff --git a/plugins/style/style.h b/plugins/style/style.h index 7f581a2..c0f3d4e 100644 --- a/plugins/style/style.h +++ b/plugins/style/style.h @@ -1,5 +1,5 @@ #pragma once -#define STYLE_N 8048 + 1000 +#define STYLE_N 8049 + 1000 void read_style_js(char* string); diff --git a/rosenrot4_greenfield.c b/rosenrot4_greenfield.c index b2a3807..d66fa90 100644 --- a/rosenrot4_greenfield.c +++ b/rosenrot4_greenfield.c @@ -114,6 +114,42 @@ void notebook_create_new_tab(GtkNotebook* notebook, const char* uri) } } +/* Listen to keypresses */ + +static int handle_signal_keypress(GtkEventControllerKey* event_controller, int keyval, int keycode, + GdkModifierType state, GtkNotebook* notebook) +{ + + printf("New keypress!\n"); + /* + if (1) { + printf("Keypress state: %d\n", state); + printf("Keypress value: %d\n", keyval); + } + + for (int i = 0; i < sizeof(shortcut) / sizeof(shortcut[0]); i++){ + if ((state & shortcut[i].mod || shortcut[i].mod == 0x0) && keyval == shortcut[i].key) { + return handle_shortcut(shortcut[i].id, notebook); + } + } + +*/ + return 0; +} + + +static gboolean +event_key_pressed_cb (GtkWidget *drawing_area, + guint keyval, + guint keycode, + GdkModifierType state, + GtkEventControllerKey *event_controller) +{ + + fprintf(stdout, "New keypress!\n"); + return 0; +} + int main(int argc, char** argv) { // Initialize i18n support with bindtextdomain(), etc. @@ -134,6 +170,17 @@ int main(int argc, char** argv) gtk_notebook_set_show_border(notebook, false); // ... + // Listen to signals + + GtkEventController *event_controller; + event_controller = gtk_event_controller_key_new (); + + g_signal_connect_object (event_controller, "key-pressed", + G_CALLBACK (event_key_pressed_cb), + window, G_CONNECT_SWAPPED); + gtk_widget_add_controller (GTK_WIDGET (window), event_controller); + + // Show the application window gtk_window_present(GTK_WINDOW(window)); gtk_window_set_child(GTK_WINDOW(window), GTK_WIDGET(notebook)); @@ -141,6 +188,8 @@ int main(int argc, char** argv) char* first_uri = argc > 1 ? argv[1] : HOME; notebook_create_new_tab(notebook, first_uri); +fprintf(stdout, "Hello world!"); + // Enter the main event loop, and wait for user interaction while (!0) g_main_context_iteration(NULL, TRUE);