Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the text cursor position in Windows?

Tags:

python

windows

Is it possible to get the overall cursor position in Windows using the standard Python libraries?

like image 615
rectangletangle Avatar asked Sep 13 '10 07:09

rectangletangle


People also ask

How do I find the cursor position in text area?

If there is no selection, you can use the properties . selectionStart or . selectionEnd (with no selection they're equal). var cursorPosition = $('#myTextarea').

How do I find the cursor position on my screen?

Once you're in Mouse settings, select Additional mouse options from the links on the right side of the page. In Mouse Properties, on the Pointer Options tab, at the bottom, select Show location of pointer when I press the CTRL key, and then select OK. To see it in action, press CTRL.

What is text cursor movement?

The cursor moves from its current position to the next or previous character in the data stream. The character may be adjacent to the cursor's position, elsewhere in the same line, or on another line on the screen. Logical cursor movement requires scanning the data stream to find the next logical character.

How do you type a text cursor?

Pressing the left arrow key moves the cursor to the left and lets you insert text at the text cursor's position. If there was text after the cursor pressing the right arrow key moves the cursor to the right.


3 Answers

Using the standard ctypes library, this should yield the current on screen mouse coordinates without any third party modules:

from ctypes import windll, Structure, c_long, byref   class POINT(Structure):     _fields_ = [("x", c_long), ("y", c_long)]    def queryMousePosition():     pt = POINT()     windll.user32.GetCursorPos(byref(pt))     return { "x": pt.x, "y": pt.y}   pos = queryMousePosition() print(pos) 

I should mention that this code was taken from an example found here So credit goes to Nullege.com for this solution.

like image 177
Micrified Avatar answered Sep 21 '22 20:09

Micrified


win32gui.GetCursorPos(point) 

This retrieves the cursor's position, in screen coordinates - point = (x,y)

flags, hcursor, (x,y) = win32gui.GetCursorInfo() 

Retrieves information about the global cursor.

Links:

  • http://msdn.microsoft.com/en-us/library/ms648389(VS.85).aspx
  • http://msdn.microsoft.com/en-us/library/ms648390(VS.85).aspx

I am assuming that you would be using python win32 API bindings or pywin32.

like image 39
pyfunc Avatar answered Sep 21 '22 20:09

pyfunc


You will not find such function in standard Python libraries, while this function is Windows specific. However if you use ActiveState Python, or just install win32api module to standard Python Windows installation you can use:

x, y = win32api.GetCursorPos()
like image 42
Michał Niklas Avatar answered Sep 21 '22 20:09

Michał Niklas