gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:15:39 26 4
gpt4 key购买 nike

是否有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`

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

最佳答案

有一个函数:gtk_widget_is_focus ()

您需要确保 parent 具有“具有焦点”属性集。
Doc。摘抄:
gtk_widget_is_focus (GtkWidget *widget);

Determines if the widget is the focus widget within its toplevel. (This does not mean that the “has-focus” property is necessarily set; “has-focus” will only be set if the toplevel widget additionally has the global input focus.)



如果要在窗口聚焦时接收事件,请注册 enter-notify-event( signal)的回调

在链接的文档中。在 enter-notify-event后仅三点就是您想要的部分:

The “focus” signal



抱歉,我应该第一次提到这个事件。

关于c++ - 检查GTK窗口是否具有键盘和鼠标焦点窗口C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62019401/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com