检查GTK窗口是否有键盘和鼠标焦点窗口C++。

问题描述 投票:1回答:1

有没有一个GTK3函数可以检测窗口是否有焦点?我目前正在使用下面的代码。

#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
GtkWidget *LinuxWindow;
static void buttonMessage(GtkWidget *widget, gpointer data)
{
  g_print("Yay, you clicked me!\n");
}
int main() {
  GtkWidget *Box, *Button;
  int argC = 0;
  char **argV;
  // Setup the window and fixed grid
  gtk_init(&argC, &argV);
  LinuxWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  Box = gtk_fixed_new();
  // Set the title
  gtk_window_set_title(GTK_WINDOW(LinuxWindow), "Title");
  // Setup the window events
  gtk_widget_show_all(LinuxWindow);
  g_signal_connect(G_OBJECT(LinuxWindow), "destroy", G_CALLBACK(gtk_main_quit),
                   NULL);
  // Add controls
  Button = gtk_button_new_with_label("Click Me!");
  g_signal_connect(Button, "clicked", G_CALLBACK(buttonMessage), NULL);
  gtk_fixed_put(GTK_FIXED(Box), Button, 20, 20);
  gtk_fixed_move(GTK_FIXED(Box), Button, 20, 20);
  gtk_widget_set_size_request(Button, 30, 100);
  gtk_container_add(GTK_CONTAINER(LinuxWindow), Box);
  gtk_widget_show_all(LinuxWindow);
  // Create a dialog
  GtkWidget *dialog;
  dialog = gtk_message_dialog_new(
      GTK_WINDOW(LinuxWindow), GTK_DIALOG_DESTROY_WITH_PARENT,
      GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "Hello and welcome to my GTK GUI...", NULL);
  gint ret = gtk_dialog_run(GTK_DIALOG(dialog));
  gtk_widget_destroy(GTK_WIDGET(dialog));
  printf("%i", ret);
  // Add the fixed grid and go the to the main window loop

  gtk_main();

  return 0;
}

我在编译时使用了

g++ -std=c++17 -m64 -o gtkTest myGtkApp.cpp -lX11 `pkg-config --cflags gtk+-3.0` `pkg-config --libs gtk+-3.0`

我想检测窗口是否有焦点,并将其打印到控制台。

c++ gtk
1个回答
1
投票

有一个函数: gtk_widget_is_focus ().

你需要确保父母有 "has-focus "属性设置。

gtk_widget_is_focus (GtkWidget *widget);

确定该小组件是否是其顶层中的焦点小组件(这并不意味着一定要设置 "has-focus "属性;"has-focus "将只设置是否为焦点小组件)。(这并不意味着 "has-focus "属性一定会被设置;"has-focus "只有在toplevel widget另外具有全局输入焦点时才会被设置。)

如果您想在窗口聚焦时接收一个事件,那么就可以为窗口的 enter-notify-event (信号)

在链接的文档中,有一节就在三点之后。enter-notify-event 这就是你想要的。

"焦点 "信号

对不起,我应该要在第一时间提到这个事件。

© www.soinside.com 2019 - 2024. All rights reserved.