0

I am trying to close a GTK window by pressing + q on Mac OS or ALT + F4 on Windows.

The code below works fine if I click into the window once – if I do not click, all commands are passed to the underlying terminal. So e.g. if I press + q, my Terminal will ask me if I want to close it (the terminal). But as soon as I click into the window using the mouse pointer, I can use + q to close this window. It feels weird that the windows do not gain focus on startup, as I feel all my normal applications do gain focus for keyboard inputs on startup.

main.cpp

// Copyright 2017 Sebastian Höffner

#include "gtk/gtk.h"


namespace {

static void cb_key_press(GtkWidget* widget, GdkEventKey* event_key) {
  switch (event_key->keyval) {
    case GDK_KEY_F4:
      if (event_key->state & GDK_MOD1_MASK) {
        gtk_main_quit();
      }
      break;
    case GDK_KEY_q: case GDK_KEY_W:
      if (event_key->state & GDK_META_MASK) {
        gtk_main_quit();
      }
      break;
  }
}

}  // namespace

int main(int argc, char** argv) {
  gtk_init(&argc, &argv);

  GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  g_signal_connect(window, "key-press-event", G_CALLBACK(::cb_key_press), NULL);
  g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

  gtk_window_fullscreen(GTK_WINDOW(window));
  gtk_window_present(GTK_WINDOW(window));

  gtk_main();

  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

# Project options
project(gtk_test VERSION 0.1 LANGUAGES CXX C)

find_package(PkgConfig)
pkg_check_modules(GTK gtk+-3.0)

include_directories(${GTK_INCLUDE_DIRS})
link_directories(${GTK_LIBRARY_DIRS})
add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
target_link_libraries(${PROJECT_NAME} ${GTK_LIBRARIES})

To compile, put both files (CMakeLists.txt and main.cpp) into the same directory, and follow these commands:

mkdir build
cd build
cmake ..
make

You can then start the program ./gtk_test (it is located in build).

Some more background

I initially started out by modifying the Getting started example but quickly found that using a GtkApplication seems to be overkill for my application (which basically should only show fullscreen images, thus OpenCV's highgui is not good enough without the Qt backend), so I adapted gtk_window_new(GTK_WINDOW_TOPLEVEL); which I found on various answers and tutorials.

This answer to How to give keyboard focus to a pop-up Gtk.Window explains how to gain exclusive keyboard access for an application, but I only want to have keyboard focus when I start the application.

UPDATE: Added CMakeLists.txt and removed the generated compilation messages. Added explanations on how to compile and run.

Sebastian Höffner
  • 1,864
  • 2
  • 26
  • 37
  • Try some of the answers [here](https://stackoverflow.com/questions/9054462/how-do-i-raise-a-window-that-is-minimized-or-covered-with-pygobject) and let me know how it works. – theGtknerd Sep 14 '17 at 17:13
  • I tried `gtk_window_set_keep_above(GTK_WINDOW(window), true);` before, I stumbled over the same question. I just tried it again, but it does not what I want. I assume it takes care of the window staying topmost, which "fullscreen" windows do anyways on MacOS. What I want is to have the keyboard focus transferred from the calling Terminal to the new window. – Sebastian Höffner Sep 15 '17 at 07:17
  • Have you also tried the `gtk_window_present()` ? – theGtknerd Sep 15 '17 at 11:30
  • Yes, it is part of the code above. – Sebastian Höffner Sep 15 '17 at 15:03
  • My bad. I do see that you have no `gtk_widget_show_all()` for the main window. – theGtknerd Sep 15 '17 at 15:41
  • I don't have it in that simple example because that will only make all widgets contained in the window visible, in a more complex example I am using it. But I also tried it for this example now, unfortunately it is still not working properly. – Sebastian Höffner Sep 15 '17 at 21:50
  • There used to be a `gtk_window_raise()` or something like that, but I can't find any reference for it. Sadly, it seems I am running out of suggestions. – theGtknerd Sep 15 '17 at 22:52
  • Thank you for trying to help still, it's highly appreciated! – Sebastian Höffner Sep 16 '17 at 05:55
  • One more suggestion : maybe it is a [focus](https://developer.gnome.org/gtk3/stable/GtkWindow.html#gtk-window-set-accept-focus) problem. – theGtknerd Sep 16 '17 at 12:02
  • I tried, with no success. Maybe it's because the default is [`true`](https://developer.gnome.org/gtk3/stable/GtkWindow.html#GtkWindow--accept-focus) already. My bet is currently [`has-toplevel-focus`](https://developer.gnome.org/gtk3/stable/GtkWindow.html#GtkWindow--has-toplevel-focus), but it's a read-only property. Maybe I can set it via a GtkBuilder with a ui xml file. Thanks again for your input. – Sebastian Höffner Sep 16 '17 at 17:00
  • Stays read-only, even with a GtkBuilder and a UI file. – Sebastian Höffner Sep 19 '17 at 15:08
  • How are you compiling this example? I use Linux and Python so I need some help in trying to duplicate your problem. – theGtknerd Sep 20 '17 at 03:39
  • I updated the example to provide a CMakeLists.txt and build instructions. – Sebastian Höffner Sep 21 '17 at 06:42
  • It works properly for me on Linux. How about another [focus](https://developer.gnome.org/gtk3/stable/GtkWindow.html#gtk-window-set-focus-on-map). – theGtknerd Sep 22 '17 at 03:09
  • I tried it, no change. According to [glade](https://glade.gnome.org) it seems to be the default anyways. But then I guess it's an OS issue. Will try it on a linux machine soon as well. – Sebastian Höffner Sep 22 '17 at 07:53

0 Answers0