Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save text cursor position in the currently focused application/control, then restore it and paste text

I would like to write 'Hello World' at the current position of the text cursor. This might be a terminal, or a textarea in Chrome in which I currently ask this question, or a Word application.

The use case is as follows:

The application is a symbol recognition system. It should be able to recognize rare symbols (like ü, ä, ö for non-germans or mathematical symbols like Σ). You can try the recognizer here.
Now I want to integrate it nicely in the operating system so that you don't have to switch to the browser, enter it, copy it, but can instead call the program with a shortcut:

  • The app gets started/pops up with a keyboard shortcut
  • saves where the text cursor was (however this could be done)
  • opens a drawing area, the user draws, clicks on close
  • the recognition is done, then the recognized sequence is written to wherever the text cursor was previously

I am interested in supporting:

  • Windows 7 (e.g. when the user has Microsoft Word opened, text editor, a browser, ...)
  • Linux Mint MATE (e.g. when the user is in a text editor, LibreOffice, a browser, ... )

enter image description here

like image 805
Martin Thoma Avatar asked Dec 09 '25 00:12

Martin Thoma


1 Answers

Using this python code:

https://github.com/SavinaRoja/PyUserInput

I can generate a string in a window as if it was typed there. Although it does fail with Unicode in Linux:

>>> time.sleep(5) ; k.type_string('ΣΣ')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pykeyboard/base.py", line 48, in type_string
    self.tap_key(i)
  File "/usr/local/lib/python2.7/dist-packages/pykeyboard/base.py", line 40, in tap_key
    self.press_key(character)
  File "/usr/local/lib/python2.7/dist-packages/pykeyboard/x11.py", line 91, in press_key
    keycode = self.lookup_character_keycode(character)
  File "/usr/local/lib/python2.7/dist-packages/pykeyboard/x11.py", line 222, in lookup_character_keycode
    keysym = Xlib.XK.string_to_keysym(special_X_keysyms[character])
KeyError: '\xce'

Not sure what the solution there is. How do you type any Unicode character into a text window anyway? There's a Gnomey-Linux standard where you can type Ctrl-Shift-u and then hex digits, then Ctrl-Shift to end. Do that with:

k.press_key(k.shift_key)
k.press_key(k.control_key)
k.type_string("u03a3")
k.release_key(k.shift_key)
k.release_key(k.control_key)

and get a Σ

The package code seems to be cross-platform, don't know if the unicode entry method is. I've only tested on Linux.

like image 141
Spacedman Avatar answered Dec 10 '25 13:12

Spacedman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!