0

I am using Linux Mint 20 with Python 3. I am a complete noob with Python. However, I am trying to set up a keyboard shortcut to display certain text wherever my cursor is at in the active window (an input box, text editor, etc.). I know how to set up shortcuts and run simple python programs in Terminal.

It seems I have to use some type of GUI interface? I have looked at GTK and pkinter, but I can't seem to figure out the correct code. Would rather use GTK or something that already comes with the system.

Thanks.

RonF
  • 1
  • Does this answer your question? [Simple way to display text on screen in Python?](https://stackoverflow.com/questions/45427411/simple-way-to-display-text-on-screen-in-python) – Random Davis Oct 30 '20 at 17:10
  • Unless I'm misunderstanding something, I don't think you need to make a GUI for the Python code. You could try simulating the keypresses to write the text, for example. – AMC Oct 30 '20 at 23:55
  • AMC, that's what I should be doing! I'm looking at pynput and keyboard (PYPl) now. I can't get either to work, but that's a different issue. Any other ones I should be looking at? – RonF Nov 01 '20 at 21:14

1 Answers1

1

You can use the pynput module to make keypresses wherever the cursor is. Here is an example:

import pynput
from pynput import keyboard

string_to_type = "Hello! How are you?"

c = keyboard.Controller()
for char in string_to_type:
    c.tap(char)

c.tap() presses and then quickly unpresses the key passed as an argument.

You can also call c.press() and c.unpress(), both of which require an argument representing a key. This enables you to hold down keys.

Sylvester Kruin
  • 3,294
  • 5
  • 16
  • 39
  • 1
    Nice answer! But only ASCII charaters are well supported. In other cases, e.g. Chinese charaters, it sometimes replace some chars with their previous one. – SMSQO May 06 '23 at 03:03