C语言 如何在Linux上的C中设置鼠标光标位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2433447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:50:40  来源:igfitidea点击:

How to set mouse cursor position in C on linux?

clinuxpointerspositionx11

提问by frx08

how can I set the mousecursor position in an X window using a C program under Linux? thanks :) (like setcursorpos() in WIN)

如何在 Linux 下使用 C 程序在 X 窗口中设置鼠标光标位置?谢谢:)(就像 WIN 中的 setcursorpos() )

EDIT: I've tried this code, but doesn't work:

编辑:我试过这段代码,但不起作用:

#include <curses.h>

main(){
 move(100, 100);
 refresh();
}

回答by Bertrand Marron

12.4 - Moving the Pointer

12.4 -移动指针

Although movement of the pointer normally shouldbe left to the control of the end user, sometimes it is necessary to move the pointer to a new position under program control.

To move the pointer to an arbitrary point in a window, use XWarpPointer().

虽然指针的移动通常应由最终用户控制,但有时需要在程序控制下将指针移动到新位置。

要将指针移动到窗口中的任意点,请使用XWarpPointer()



Example:

例子:

Display *dpy;
Window root_window;

dpy = XOpenDisplay(0);
root_window = XRootWindow(dpy, 0);
XSelectInput(dpy, root_window, KeyReleaseMask);
XWarpPointer(dpy, None, root_window, 0, 0, 0, 0, 100, 100);
XFlush(dpy); // Flushes the output buffer, therefore updates the cursor's position. Thanks to Achernar.

回答by Achernar

This is old, but in case someone else comes across this issue. The answer provided by tusbar was correct but the command XFlush(dpy)must be added at the end to update the cursor's position. The libraries needed are: X11/X.h, X11/Xlib.h, X11/Xutil.h.

这是旧的,但万一其他人遇到这个问题。tusbar 提供的答案是正确的,但必须在末尾添加命令XFlush(dpy)以更新光标的位置。需要的库有:X11/Xh、X11/Xlib.h、X11/Xutil.h。

    int main(int argc, char *argv[]){
         //Get system window
         Display *dpy;
         Window root_window;

         dpy = XOpenDisplay(0);
         root_window = XRootWindow(dpy, 0);
         XSelectInput(dpy, root_window, KeyReleaseMask);

         XWarpPointer(dpy, None, root_window, 0, 0, 0, 0, 100, 100);

         XFlush(dpy);

         return 0;}

回答by mctylr

You want to write a X11 programthat uses the call XWarpPointerfunction to move the point to a relative or global position. (Xlib Programming Manual, Vol 1)

您想编写一个X11 程序,该程序使用 callXWarpPointer函数将点移动到相对或全局位置。(Xlib 编程手册,第 1 卷)

In general, using Xlib for programming the X Window System, is the most basic, and quite low-level interface for graphical programming on a Unix or Linux system. Most applications developed nowadays using a higher level library such as GTKor Qtfor developing their GUI applications.

通常,使用 Xlib 对 X Window 系统进行编程,是在 Unix 或 Linux 系统上进行图形编程的最基本且相当低级的界面。现在开发的大多数应用程序都使用更高级别的库(例如GTKQt)来开发其 GUI 应用程序。

Curses or NCurses (New Curses) is for programming terminal-oriented interfaces, so are not useful in this case.

Curses 或 NCurses (New Curses) 用于编程面向终端的接口,因此在这种情况下没有用。

回答by plan9assembler

use Jordan Sissel's excellent utility xdotool.

使用 Jordan Sissel 的优秀实用工具 xdotool。

http://www.semicomplete.com/projects/xdotool/

http://www.semicomplete.com/projects/xdotool/

it provide XWarpPointer wrapper function like xdo_mousemove(), here is some example:

它提供了像 xdo_mousemove() 这样的 XWarpPointer 包装函数,下面是一些例子:

Display *display = NULL;
xdo_t *xdo = NULL;

void mouse_left_down(int x, int y)
{
  xdo_mousemove(xdo, x, y, 0)
  xdo_mousedown(xdo, CURRENTWINDOW, Button1); 
}

void mouse_left_up(int x, int y)
{
  xdo_mouseup(xdo, CURRENTWINDOW, Button1, 1, 0); 
}

void mouse_left_double_click(int x, int y)
{
  xdo_mousemove(xdo, x, y, 0);
  xdo_click_multiple(xdo, CURRENTWINDOW, Button1, 1, 0);
  doubleclick = TRUE;
}

int main()
{

  display = XOpenDisplay(NULL);
  if(display == NULL)
  {
    fprintf(stderr, "can't open display!\n");
    return -1;
  }
  xdo = xdo_new((char*) display);

  //some task here
  // ...

  return 0;
}

回答by Blindy

You can use XWarpPointerto move the mouse cursor in an X window.

您可以使用XWarpPointer在 X 窗口中移动鼠标光标。

XWarpPointer(display, src_w, dest_w, src_x, src_y, src_width, src_height, dest_x, 
                dest_y)
        Display *display;
        Window src_w, dest_w;
        int src_x, src_y;
        unsigned int src_width, src_height;
        int dest_x, dest_y;

回答by aib

All modern terminals should support ANSI escape sequences. For anything more complicated (and more portable), however, you should look into using a library such as ncurses.

所有现代终端都应该支持ANSI 转义序列。但是,对于更复杂(和更便携)的任何内容,您应该考虑使用诸如ncurses 之类的库。