I have a gtk entry right below the scrolled window which has the default focus , left and right keys move the cursor in the entry ,I am able to catch the key press events for up and down arrow keys but don't know how to scroll the scrolled window, referred many websites none of them were clear or explained only in parts.
Below are some of the pages I went through: https://mail.gnome.org/archives/gtk-devel-list/2002-February/msg00104.html
https://developer.gnome.org/gtkmm-tutorial/stable/sec-keyboardevents-overview.html.en
tried using gtk_scrolled_window_set_vadjustment() couldn't get it working. The official page says GTK_SCROLL_STEP_UP is deprecated but doesn't say what should be used instead.
Every answer would be very appreciated.Thanks
bool Method::cb_MPWindow(GtkWidget *wgt, GdkEventKey *event, MethodSelect *ms)
{
if(event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_Down)
{
g_signal_emit_by_name(ms->ScrolledWindow, "scroll-child",(event->keyval == GDK_KEY_Up)?GTK_SCROLL_STEP_UP:GTK_SCROLL_STEP_DOWN);
//The above line works in gtk 3.14.5 but crashes the app in 3.24.5
return TRUE;
}
return FALSE;
}
In order to scroll the window with the keyboard, you need to:
gtk_scrolled_window_get_vadjustment()
orgtk_scrolled_window_get_hadjustment()
.value
(the current scroll position),step-increment
(how much to scroll by line), andpage-increment
(how much to scroll by page).value
and then set the new value withgtk_adjustment_set_value()
.The the window will scroll when change when you set the value. Typically the line increment is used when navigating with the arrow keys, while the page increment when using the Page Up/Down keys. You add them when scrolling down, and subtract while scrolling down. It is worth noting that the increments change dynamically based on the window size, so you do not need to set them manually.
Here is my code (in C). First setting up the callback:
And then the callback function:
For convenience, here is a list of keyboard macros that GTK accepts: https://github.com/GNOME/gtk/blob/main/gdk/gdkkeysyms.h