asweigart / pyautogui

A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
BSD 3-Clause "New" or "Revised" License
10.45k stars 1.26k forks source link

Question: Can I insert text at the current cursor position? #674

Open dgleba opened 2 years ago

dgleba commented 2 years ago

If I should not be asking this here, please tell me and I will post where you suggest. I am just getting started with this package.

Hello pyautogui people:

In Pop!_os 21.10 I created a keyboard shortcut in the settings panel 'ctrl'-'/' to run python3 /home/dg/scuts/p2-pyautogui.py

In /home/dg/scuts/p2-pyautogui.py I used the following:

import pyautogui, time 
time.sleep(0.3)
pyautogui.write("el0 n 123. 12345", interval=0.005)

In gedit, If I hold down the control key too long, it seems to execute 'ctrl'-'n' and open a new gedit window. Sometimes I get the emoticon input window, which I think is 'ctrl'-'.'

I want to make it more mistake-proof for entering text like date-time for example using a hotkey combination.

What am I doing wrong?

InfamousPlatypus commented 2 years ago

To clarify, you are hitting a hotkey and you want it do do something, say put in the date?

dgleba commented 2 years ago

Yes, I am hitting ctrl - / .

It is supposed to enter el0 n 123. 12345 in the gedit screen or whatever screen accepts text, but it opens a new window sometimes. It seems like it does that if I hold the ctrl key too long.

Once I got it all working with el0 n 123. 12345 or other test text, I want to graduate to entering the date-time, etc.

JayRizzo commented 2 years ago

Hi @dgleba!

I hope you are doing well. We Need more code as an example to understand what you are trying to do.

Is this what you are saying?

import pyautogui
import time 

pyautogui.hotkey('ctrl', '/')
time.sleep(0.3)
pyautogui.write("el0 n 123. 12345", interval=0.005)
time.sleep(0.3)
pyautogui.hotkey('enter')
time.sleep(0.3)
# pyautogui.hotkey('return')

Just dont try doing this pyautogui.hotkey('ctrl', 'el0 n 123. 12345') it doesn't work that way.

There are too many ways to program for, and no direct answer.

Depending on the different

Needless to say there are a number of ways this can be hiccuped... It is just tackling one at a time.