- How to set mouse cursor position in C on linux?
- 6 Answers 6
- How do I move the cursor in Linux command line?
- What function is used for moving the cursor to the specified row?
- How do you move the cursor in Word?
- How do I clear the command line?
- How do I press Return in Linux terminal?
- How to move the cursor to a specific row and column?
- How to set the cursor position in tput?
- How to set mouse cursor position in c on linux?
- Method 1: Using the Xlib Library
- Method 2: Using the XTest Extension
- Step 1: Include the necessary headers
- Step 2: Open a connection to the X server
- Step 3: Get the root window
- Step 4: Move the mouse cursor to the desired position
- Step 5: Close the connection to the X server
- How to set cursor position with command/script?
- 1 Answer 1
- Moving the mouse to a defined (absolute) position
- Straightforward version (move cursor to the center of the left screen)
- Extended version (optional arguments x, y)
- Explanation
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
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.
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 do I move the cursor in Linux command line?
Moving lines also requires two commands: dd (“delete”) and either p or P . To move one line, position the cursor anywhere on the line and type dd . For example, to delete 5 lines, type 5dd .
What function is used for moving the cursor to the specified row?
The object of ResultSet maintains a cursor pointing to a row of a table….Commonly used methods of ResultSet interface.
1) public boolean next(): | is used to move the cursor to the one row next from the current position. |
---|---|
3) public boolean first(): | is used to move the cursor to the first row in result set object. |
Which of the following method is used to move the cursor position to next row?
Public methods | |
---|---|
abstract boolean | moveToLast() Move the cursor to the last row. |
abstract boolean | moveToNext() Move the cursor to the next row. |
abstract boolean | moveToPosition(int position) Move the cursor to an absolute position. |
abstract boolean | moveToPrevious() Move the cursor to the previous row. |
How do I clear the command line in Linux?
You can use Ctrl+L keyboard shortcut in Linux to clear the screen. It works in most terminal emulators. If you use Ctrl+L and clear command in GNOME terminal (default in Ubuntu), you’ll notice the difference between their impact.
How do you move the cursor in Word?
The remaining cursor keys are the Page Up or PgUp key and the Page Down or PgDn key….How to Move the Insertion Pointer in Word 2010.
Press This Key or Combination | To Move the Insertion Pointer |
---|---|
Ctrl+down arrow | Down to the start of the next paragraph |
Ctrl+→ | Right to the start (first letter) of the next word |
Ctrl+ | Left to the start (first letter) of the previous word |
How do I clear the command line?
- In Command Prompt, type: cls and press Enter. Doing this clears the entire application screen.
- Close and reopen Command Prompt. Click the X on the top right of the window to close it, then reopen it as usual.
- Press the ESC key to clear the line of text and move back to the Command Prompt.
How do I press Return in Linux terminal?
Ctrl+XX: Move between the beginning of the line and the current position of the cursor. This allows you to press Ctrl+XX to return to the start of the line, change something, and then press Ctrl+XX to go back to your original cursor position.
Is there any command that moves cursor to specific position in terminal?
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.
How to get the cursor position in Bash?
Pv = vertical position (row) Ph = horizontal position (column) The tput commands are what you need to use. simple, fast, no output to the screen. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share your research!
How to move the cursor to a specific row and column?
I am running Vim version 7.3.429. Try a number followed by a pipe to get to the specified column in that line. 80| should get you to position 80 in that line. EDIT: If you are looking to go to a specific x,y position, I am not sure on that one.
How to set the cursor position in tput?
1. Set the Cursor Position using tput cup You can move the cursor to a specific row and column using tput cup. Following example positions the cursor at row 2 and column 3. 2. Clear the Screen Using tput clear If you are in middle of the terminal screen, tput clear will clear the screen and put you at the top of the terminal screen. 3.
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
How to set cursor position with command/script?
I have a script which resets some things, and at the end of it I need it to set the cursor to certain coordinates, either to a custom set or to the centre of the screen (where it is reset to by default when restarting gnome-shell for instance). How can this be achieved? The solution would have to work for all display sizes and be able to automatically get the data and do all the maths etc involved. I am running Ubuntu GNOME 16.04 with GNOME 3.20.
Do I understand that it symply needs to set the cursor in the middle op the screen? If so, which one if multiple?
@JacobVlijm: That as well as being able to be given custom coordinates for where to reset it two. Perhaps it should be given the custom coordinates as arguments, but if there are no arguments given then it should assume the centre.
1 Answer 1
Moving the mouse to a defined (absolute) position
..is simply done by the command (e.g.):
xdotool mousemove 200 200
To move the mouse to the centre of the screen however is a relative command, for which we need to read the screen’s information and do some calculations. This is done in the two small scripts below.
Straightforward version (move cursor to the center of the left screen)
To move the mouse to the center of the (leftmost) screen, use the script below:
#!/usr/bin/env python3 import subprocess xr = [s for s in subprocess.check_output("xrandr").decode("utf-8").split() if "+0+" in s] scr = [int(n)/2 for n in xr[0].split("+")[0].split("x")] subprocess.Popen(["xdotool", "mousemove", str(scr[0]), str(scr[1])])
sudo apt-get install xdotool
python3 /path/to/center_screen.py
Extended version (optional arguments x, y)
If arbitrary coordinates are optional, use:
#!/usr/bin/env python3 import subprocess import sys if sys.argv[1:]: scr = [sys.argv[1], sys.argv[2]] else: xr = [s for s in subprocess.check_output("xrandr").decode("utf-8").split() if "+0+" in s] scr = [str(int(n)/2) for n in xr[0].split("+")[0].split("x")] subprocess.Popen(["xdotool", "mousemove", scr[0], scr[1]])
This version will move the cursor to the center of the screen, when run without arguments, or to an arbitrary position, when run with arguments, e.g.:
python3 /path/to/center_screen.py 200 200
Explanation
In the output of the command: xrandr , all we need to find is the string like:
. which contains the data on the leftmost screen ( +0+ ). both figures in 1680×1050 are then to be divided by two, to be used in:
is then to decide wether the given arguments should be used or the calculated ones.