gpt4 book ai didi

c - 如何将合成鼠标事件插入 X11 输入队列

转载 作者:太空狗 更新时间:2023-10-29 16:42:51 25 4
gpt4 key购买 nike

我有一个运行 Linux/X11 的嵌入式设备,它连接到一个通过 USB 连接提供触摸事件的设备。此设备未被识别为任何形式的标准指针/鼠标输入。我想做的是找到一种方法,当外部设备报告事件时,将鼠标事件“注入(inject)”到 X11 中。

这样做将消除我的应用程序(使用 Gtk+ 用 C 编写)通过 Gtk+ 调用伪造鼠标按下的需要。

如果可以做到这一点,我的 Gtk+ 应用程序就不需要知道或关心生成触摸事件的设备。它只会作为标准鼠标事件出现在应用程序中。

有人知道如何将合成鼠标事件插入 X11 吗?

现在我正在做以下工作,但不是最佳的。

GtkWidget *btnSpin;     /* sample button */

gboolean buttonPress_cb( void *btn );
gboolean buttonDePress_cb( void *btn );


/* make this call after the device library calls the TouchEvent_cb() callback
and the application has determined which, if any, button was touched

In this example we are assuming btnSpin was touched.

This function will, in 5ms, begin the process of causing the button to do it's
normal animation ( button in, button out effects ) and then send the actual
button_clicked event to the button.
*/
g_timeout_add(5, (GSourceFunc) buttonPress_cb, (void *)btnSpin);


/* this callback is fired 5ms after the g_timeout_add() function above.
It first sets the button state to ACTIVE to begin the animation cycle (pressed look)
And then 250ms later calls buttonDePress_cb which will make the button look un-pressed
and then send the button_clicked event.
*/
gboolean buttonPress_cb( void *btn )
{

gtk_widget_set_state((GtkWidget *)btn, GTK_STATE_ACTIVE);
g_timeout_add(250, (GSourceFunc) buttonDePress_cb, btn);


return( FALSE );
}

/* Sets button state back to NORMAL ( not pressed look )
and sends the button_clicked event so that the registered signal handler for the
button can be activated
*/
gboolean buttonDePress_cb( void *btn )
{

gtk_widget_set_state( btn, GTK_STATE_NORMAL);
gtk_button_clicked( GTK_BUTTON( btn ));

return( FALSE );
}

最佳答案

Linux 输入系统有一个名为 uinput 的输入设备的用户空间实现工具。您可以编写一个后台程序,使用您设备的回调库将输入事件发送到内核。 X 服务器(假设它正在使用 evdev 输入模块)然后将像处理任何其他鼠标事件一样处理这些事件。

有一个图书馆叫libsuinput这使得这很容易做到。它甚至包括 example mouse input program您可能可以将其用作模型。但是,由于您的设备是基于触摸的设备,它可能会使用绝对轴(ABS_X、ABS_Y)而不是相对轴(REL_X、REL_Y)。

关于c - 如何将合成鼠标事件插入 X11 输入队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10319519/

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