4

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

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • To the person who voted to close: Why is this too broad? – Martin Thoma Feb 25 '15 at 14:07
  • 1
    the recognizer in very nice, did you release some code? – lelloman Feb 25 '15 at 14:08
  • @lelloman I released all code which I was allowed to release: http://martin-thoma.com/write-math/ – Martin Thoma Feb 25 '15 at 14:10
  • @moose There are many ways you could start. If you are looking for recommendations of technology to use, that is also off-topic. You need to have started and have specific python related problems. – Peter Wood Feb 25 '15 at 14:11
  • @PeterWood I don't know a single way to start. If there are many, please add an answer. – Martin Thoma Feb 25 '15 at 14:12
  • @moose I don't know where to start either. I would look at windows first, as that's the platform I happen to be on today. I'd search for supporting APIs maybe open source libraries. That's your job though, now. – Peter Wood Feb 25 '15 at 14:15
  • 2
    If I understand the question, you are asking how to _paste_ some text ('hello world', or the recognized symbol) into the current cursor position in whatever application/widget currently has the focus? Like, you click some button in your python app, then quickly change the focus, and then it pastes to whatever now has the focus? Wouldn't it be easier to just provide some "copy to clipboard" functionality and leave the actual pasting to the user? – tobias_k Feb 25 '15 at 14:17
  • There's a few solutions here if you search for "generate key events", for example http://stackoverflow.com/questions/5714072/simulate-keystroke-in-linux-with-python and others for windows. No perfect duplicates yet, and the goalposts seem to move.. – Spacedman Feb 25 '15 at 14:18
  • 1
    @tobias_k Yes. The idea is that my Python app get started with a keyboard shortcut, saves where the text cursor is (however this could be done) opens a drawing area, the user draws, clicks on close, the recognition is done, then the recognized symbol is written to the stored text cursor position. – Martin Thoma Feb 25 '15 at 14:19
  • @Spacedman The problem with "generate key events" is that there might not be key events to produce what I want. For example, what would be the key event for the unicode symbol Σ? – Martin Thoma Feb 25 '15 at 14:21
  • The [`SendInput` function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx) can be used in Windows to send input as if it came from the keyboard. – Mark Ransom Feb 25 '15 at 15:04

1 Answers1

1

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.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • Thank you very much (+1 and accept)! This + the idea to copy the string to the clipboard (and using ctrl + v) seems to do the trick! – Martin Thoma Feb 25 '15 at 15:02
  • 1
    @moose You still need a way to remember the previously focused control and switch back to it – ivan_pozdeev Feb 25 '15 at 15:11
  • 1
    It turns out that I don't need to do that. I close my window, the focus goes back to where it was before, I paste the text. (At least on Ubuntu MATE, I will test it on other systems.) – Martin Thoma Feb 25 '15 at 15:12
  • What a shame that repo is dead. Seem to have had many more useful features than the one that is pointed to by jumping the hoops and end up at [pynput](https://github.com/moses-palmer/pynput). – not2qubit Jan 16 '22 at 23:36