Linux set cursor position

How to set mouse cursor position in C on linux?

how can I set the mouse cursor position in an X window using a C program under Linux? thanks 🙂 (like setcursorpos() in WIN) EDIT: I’ve tried this code, but doesn’t work:

in an X window.. but I don’t have to get the cursor position, I have to set it everywhere in the screen

(I’ve edited your question for you; you really wanted to do that when you replied to my comment.) You see the value of being specific. 🙂 You now have three answers completely unrelated to the question (they’re all about setting cursor position in terminal windows).

In X Window System, the mouse cursor refers to the graphic icon, whereas you seem to want to move the pointer itself (which moves the «hotspot» as well as also moving the cursor icon).

6 Answers 6

Although movement of the pointer normally should be 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().

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. 

thanks but it doesn’t work: I edited the 2nd argument of XWarpPointer from NULL to None, I get no errors while compiling, but cursor doesn’t move

It didn’t compile for me either and I think it’s because of missing libraries. I tried including the libraries mentioned by @Achernar, but my compiler still can’t find the references to XOpenDisplay and the other library calls. I’m including them as follows (obviously, not all in one line, but I’m having trouble formatting my comment): #include #include #include #include Any ideas as to what’s wrong here?

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.

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;>

PS: You can use this command to build the code gcc main.c -lX11

You want to write a X11 program that uses the call XWarpPointer function to move the point to a relative or global position. (Xlib Programming Manual, Vol 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 GTK or Qt for developing their GUI applications.

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

Читайте также:  Mounting exfat on linux

use Jordan Sissel’s excellent utility xdotool.

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

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; > 

Источник

How to set mouse cursor position in c on linux?

Setting the mouse cursor position in C on Linux is a task that requires the use of specific libraries and functions. The process can be different depending on the operating system you are using and the programming environment you are working in. In this article, we will focus on setting the mouse cursor position in C on Linux systems. The following methods will be covered:

Method 1: Using the Xlib Library

To set the mouse cursor position in C on Linux using the Xlib library, you can follow these steps:

Display *display = XOpenDisplay(NULL); if (display == NULL)  fprintf(stderr, "Cannot open display\n"); exit(1); >
Window root = XDefaultRootWindow(display);
int x = 100; // set the x-coordinate int y = 100; // set the y-coordinate XWarpPointer(display, None, root, 0, 0, 0, 0, x, y); XFlush(display); // flush the output buffer to update the cursor position
#include #include #include  int main()  Display *display = XOpenDisplay(NULL); if (display == NULL)  fprintf(stderr, "Cannot open display\n"); exit(1); > Window root = XDefaultRootWindow(display); int x = 100; // set the x-coordinate int y = 100; // set the y-coordinate XWarpPointer(display, None, root, 0, 0, 0, 0, x, y); XFlush(display); XCloseDisplay(display); return 0; >

This code opens a connection to the X server, gets the root window, sets the mouse cursor position to (100, 100), flushes the output buffer to update the cursor position, and then closes the connection to the X server.

Method 2: Using the XTest Extension

To set the mouse cursor position in C on Linux using the XTest Extension, you can follow these steps:

Step 1: Include the necessary headers

Step 2: Open a connection to the X server

Display *display = XOpenDisplay(NULL);

Step 3: Get the root window

Window root = XDefaultRootWindow(display);

Step 4: Move the mouse cursor to the desired position

XTestFakeMotionEvent(display, -1, x, y, 0); XFlush(display);

In the above code, x and y are the coordinates of the desired position.

Step 5: Close the connection to the X server

#include  #include  int main()  Display *display = XOpenDisplay(NULL); Window root = XDefaultRootWindow(display); int x = 100, y = 100; XTestFakeMotionEvent(display, -1, x, y, 0); XFlush(display); XCloseDisplay(display); return 0; >

This code will move the mouse cursor to the position (100, 100) on the screen.

Note that you need to link your program with the X11 and Xtst libraries:

gcc -o program program.c -lX11 -lXtst

Источник

Set and read cursor position in terminal (windows and linux)

I wanted to overwrite the output for my sensor readings and do this in a platform independent way, via C/C++.

On windows, you can simply [1]:

#include COORD GetCursorPosition() < HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO bufferInfo; GetConsoleScreenBufferInfo(h, &amp;bufferInfo); return bufferInfo.dwCursorPosition; >void SetCursorPosition(int XPos, int YPos)

On linux, you can use terminal commands (this possibly also works on other systems) to set the position. Unfortunately, reading the position is not so easy. The simplest way is to count the newlines and move the cursor, \r and up as many times as necessary, see commands below [2].

#include void SetCursorPosition(int XPos, int YPos) < printf("\033[%d;%dH",YPos+1,XPos+1); >void getCursor(int* x, int* y) < printf("\033[6n"); /* This escape sequence !writes! the current coordinates to the terminal. We then have to read it from there, see [4,5]. Needs , and some others */ scanf("\033[%d;%dR", x, y); > // Or platform independent using (n)curses #include // similar name for windows initscr(); // is required before getxy call getyx(curscr, y, x); void getyx(WINDOW *win, int y, int x);
- Position the Cursor: \033[;H Or \033[;f puts the cursor at line L and column C. - Move the cursor up N lines: \033[A - Move the cursor down N lines: \033[B - Move the cursor forward N columns: \033[C - Move the cursor backward N columns: \033[D - Clear the screen, move to (0,0): \033[2J - Erase to end of line: \033[K - Save cursor position: \033[s - Restore cursor position: \033[u

Источник

Posix command that moves cursor to specific position in terminal window

in school we have been assigned a homework in which we are suppose to print an ascii art into a terminal window. A input is data in format [x_coordinate, y_coordinate, char_ascii_value] (there is no data for coordinates where shouldn’t be print any character). I don’t have any trouble actually doing it but I guess I am simply too lazy to go into for cycle and print an empty space every time there is no data for character, then go to another line in terminal and do the same, etc. So I was thinking that, there must be an easier way! Since we are allowed to work only with commands which are in POSIX, is there any command that allows you to move cursor to specific position in terminal? I ran into the command named tput and tput cup does exactly what I need but I am not quite sure if tput cup is in POSIX. P.S. Please don’t take this like some kind of cheating. I am just trying to find a way to make my life easier instead of brainless writing code.

POSIX doesn’t handle that. But there is a standards committee that does — that’s ANSI. And yeah, anything that can write to stdout can do it. tput is POSIX, but it might just be the least standardized of their listed tools. Basically you can do: printf \\33\[ ?[ABCD] , where ? is a number for how many cursor positions you want to move — A goes up, B down, C right, D left. And use \\337 or \\338 for saving/restoring current cursor state respectively.

The arguments to tput allowed by POSIX don’t include anything that does absolute or relative cursor motion. I think you’ll need to write C code, using the curses or terminfo libraries, if you want to do that.

@MarkPlotnick — the arguments to tput are not constrained by POSIX. In fact, they’re deliberately unspecified: The tput utility shall display terminal-dependent information. The manner in which this information is retrieved is unspecified. The information displayed shall clear the terminal screen, initialize the user’s terminal, or reset the user’s terminal, depending on the operand given. The exact consequences of displaying this information are unspecified.

There’s an ANSI escape sequence to specify an absolute position, \\33\[r;cH where r is the desired row and c the desired column, both starting at 1. It’s widely supported but isn’t POSIX.

2 Answers 2

As mikeserv explains, POSIX doesn’t specify tput cup . POSIX does specify tput but only minimally. That said, tput cup is widely supported!

The standardised way of positioning the cursor is using ANSI escape sequences. To position the cursor you’d use something like

which will print $CHAR at line $Y and column $X . A more complete solution would be

printf "\337\33[%d;%dH%s\338" "$Y" "$X" "$CHAR" 

which will restore the cursor position.

tput is left vague and minimal in POSIX because there is a more detailed specification in X/Open Curses:

There appears to be no direct link to an HTML version of the latter (in particular the command-line tput ), but it is more detailed (about twice as long). Quoting from the description in X/Open Curses:

7319 When XCURSES is supported, this description for the tput utility replaces that in the XC 7320 specification. 7321 The tput utility uses the terminfo database to make the values of terminal-dependen 7322 capabilities and information available to the shell (see sh in the XCU specification); to clear 7323 initialize, or reset the terminal; or to return the long name of the requested terminal type. Th 7324 tput utility outputs a string if the capability attribute (capname) is of type string, or an integer i 7325 the attribute is of type integer. If the attribute is of type boolean, tput simply sets the exit statu 7326 (0 for TRUE if the terminal has the capability, 1 for FALSE if it does not), and produces n 7327 output. 

the program will retrieve any value from the terminal database. Most of the platforms which you use provide an implementation of X/Open Curses. The fine details may differ, of course. On some platforms you may encounter a version of tput which uses termcap names rather than terminfo. But you are not likely to encounter that on an assignment using «POSIX», and in any case, you can accomplish the same goal, using a slightly different vocabulary.

However, neither curses nor ANSI escape sequences are part of POSIX. Escape sequences are standardized in ECMA-48:

As a rule, POSIX does not overlap much with other standards (you will find most exceptions to that rule versus the C standard). Likewise, X/Open Curses does not overlap much with ECMA-48: the form and content of escape sequences are not detailed in that document.

Strictly speaking, you cannot do your assignment using only POSIX. You can only do this using POSIX plus the usual assortment of related standards for which there is an implementation on your system.

The reason for applications such as tput (and libraries such as curses ) is to provide a layer to hide details and inconsistencies across implementations. POSIX only goes so far, and omits most of the interesting features of an operating system, e.g., user management, security, and of course managing terminals. Even with escape sequences, there are several ways to move the cursor on various terminals. Here are a few terminfo summaries of those:

 carriage_return cr cr carriage return (P*) (P*) column_address hpa ch horizontal position #1, absolute (P) cursor_address cup cm move to row #1 col- umns #2 cursor_down cud1 do down one line cursor_home home ho home cursor (if no cup) cursor_left cub1 le move left one space cursor_mem_address mrcup CM memory relative cur- sor addressing, move to row #1 columns #2 cursor_right cuf1 nd non-destructive cursor_to_ll ll ll last line, first column (if no cup) cursor_up cuu1 up up one line space (move right one space) parm_left_cursor cub LE move #1 characters to the left (P) parm_right_cursor cuf RI move #1 characters to the right (P*) restore_cursor rc rc restore cursor to position of last row_address vpa cv vertical position #1 absolute (P) save_cursor sc sc save current cursor position (P) tab ht ta tab to next 8-space hardware tab stop save_cursor 

Источник

Оцените статью
Adblock
detector