用GTK查询光标位置 [英] Query cursor position with GTK

查看:84
本文介绍了用GTK查询光标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用GTK C库查询光标位置?我正在编写一个程序,需要实时读取光标的位置(通过回调函数或通过轮询值以查找更改).我已经浏览了GTK文档,但是找不到任何明显的可以显示光标位置的东西.

Is there a way to query the cursor position using the GTK C libraries? I am writing a program where I need to read out the position of the cursor in real time (either through a callback function or by polling the values to look for change). I have scanned through the GTK documentation, but I couldn't find anything obvious that exposes the cursor position.

我目前正在使用GTK C库编写该程序,因为a)我已经用C语言编写了一些代码来与低级的Raspberry Pi外设接口,并且我宁愿不使用其他语言重写它.躲开它.低级代码基于mmap(),我相信它仍可与C ++一起使用,因此,如果推入推送,我想我可以使用wxWidgets或QT之类的其他GUI库用C ++重写它.(是否有使用这些库读取光标位置的简便方法?)

I am currently writing this program using the GTK C libraries because a) I already have some code written in C to interface with the low level Raspberry Pi peripherals, and I'd rather not rewrite it for a different language if I can avoid it. The low level code is based on mmap(), which I believe still works with C++, so if push comes to shove, I supposed I can rewrite it in C++ with some other GUI library like wxWidgets or QT. (Are there easy ways to read the cursor position with those libraries?)

推荐答案

您可以查看那些在面板上吸引眼球的玩具小部件的工作方式,例如 xfce4-眼睛插件.

You can take a look at how it's done in those toy widgets which draw eyes on the panel, e.g. xfce4-eyes-plugin.

gboolean
where (void)
{
  gint x, y;
  GdkWindow *window;
  GdkDevice *mouse_device;

#if GTK_CHECK_VERSION (3,20,0)
  GdkSeat *seat = gdk_display_get_default_seat (gdk_display_get_default ());
  mouse_device = gdk_seat_get_pointer (seat);
#else
  GdkDeviceManager *devman = gdk_display_get_device_manager (gdk_display_get_default ());
  mouse_device = gdk_device_manager_get_client_pointer (devman);
#endif

  window = gdk_display_get_default_group (gdk_display_get_default ());
  gdk_window_get_device_position (window, mouse_device, &x, &y, NULL);
  g_message ("pointer: %i %i", x, y);

  return G_SOURCE_CONTINUE;
}

这篇关于用GTK查询光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆