How to press Enter using PyAutoGUI - python

please tell me how to press the Enter button using the PyAutoGUI library. I've tried everything, but nothing is pressed. Can you suggest how to do it?

Use pyautogui.press(“enter”) or pyautogui.hotkey(“enter”)
for pressing 3 times:
use pyautogui.press(“enter”, presses=3)
or
for i in range(3):
pyautogui.press(“enter”)
for pressing lots of keys:
pyautogui.press([“enter”, “shift”])
or
for key in [“enter”, “shift”]:
pyautogui.press(key)
dispatch user holding down the key until keyup:
pyautogui.keyDown(“enter”)
and for keyup:
pyautogui.keyUp(“enter”)
and also one thing, if you’re used keyDown, you can still use pyautogui.press(“enter”) too :D
If you want to know more go to https://pyautogui.readthedocs.io/en/latest/keyboard.html

Short answer
pyautogui.press('enter')
or
pyautogui.write('\n')
source
If not working, could be because the mouse cursor is not on the desired place, maybe you would need to first click over the app you want to enter with for example pyautogui.click(100, 200); where (100,200) are the X,Y coordinates of screen, you will need to locate where do you need that enter.
For more details, you could see this

on windows I could never get Pyautogui key presses to work. I had to use pywinauto instead. I would still use pyautogui for finding images and typing our characters but used pywinauto to press keys.
from pywinauto.keyboard import send_keys
send_keys('{ENTER}')
https://pywinauto.readthedocs.io/en/latest/code/pywinauto.keyboard.html

I had a problem like you,but i solved it by turning the keyboard language from Chinese to English ,so the enter_press is useful to send message but not write message

Related

pressing keys together in pyautogui to make them make a function?

I have been recently trying to create a program that makes new folder in python(pyautogui).
Here is my code:
import pyautogui;# import the library
pyautogui.press('ctrl');# makes our program to press 'ctrl'
pyautogui.press('n');# makes our program to press 'n'
Apparently what it does instead is pressing ctrl and n individually and I want from them to be pressed together. Please help.
According to the docs:
The press() function is really just a wrapper for the keyDown()
and keyUp() functions, which simulate pressing a key down and then
releasing it up.
As you want to combine several key presses, you need to call keyDown() and keyUp() separately:
pyautogui.keyDown('ctrl')
pyautogui.press('n')
pyautogui.keyUp('ctrl')

Make auto key and mouse press on activate window on Python

I want to write an app in Python on Windows to do some jobs repeatedly.
For example, I need to convert some files into other type. I have a software installed in Windows to do that. However, that program was designed to do it file by file. Now I want to do it automatically.
Therefore, I need to write a software to simulate the key press on active windows. There are a lot of code on autokeyboard but it only works in terminal which run Python script. Specially, after I run Python script, I minimize the terminal, then open some program then the Python script will simulate the key press and/or mouse click in this program.
I found a lot of program can do something like hotkey and after press hotkey, it will simulate some key press and mouse. So I think it is possible.
Could anyone give me a solution for that?
Thanks.
This will help you to automate:
for mouse clicks:
import pyautogui
pyautogui.click(1319, 45)
pyautogui.scroll(200)
pyautogui.hotkey("ctrlleft", "a")
For keyboard
import keyboard
# It writes the keys r, k and endofline
keyboard.press_and_release('shift + r, shift + k, \n')
keyboard.press_and_release('R, K')
# it blocks until esc is pressed
keyboard.wait('esc')
# It records all the keys until escape is pressed
rk = keyboard.record(until='Esc')
# It replay back the all keys
keyboard.play(rk, speed_factor=1)

Pyautogui - Need to hold shift and click

How does one hold shift and and click with the mouse using pyautogui?
pyautogui.hotkey('shift', click(1611, 600))
have tried the line above. But it doesn't work
The hotkey() function only works with keys. To shift-click, you need code like this:
import pyautogui
pyautogui.keyDown('shift')
pyautogui.click()
pyautogui.keyUp('shift')
I tried the solution above but for some reason keyDown('shift') didnt work. I used the solution in this thread: keyDown function not working with shift key
pyautogui.keyDown('shiftleft')
pyautogui.keyDown('shiftright')
pyautogui.hotkey('right','right','ctrl','up')
pyautogui.keyUp('shiftleft')
pyautogui.keyUp('shiftright')

PyAutoGui - Press key for X seconds

I'm currently working on a script that presses the 'w,a,s,d' keys in order to move a character in any game.
For this to work, i need to have the 'w' key pressed for a specific amount of time. How can I achieve this?
I thought of something like:
pyautogui.keyDown('w')
time.sleep(2)
pyautogui.keyUp('w')
But this just pauses the whole program and no key is being pressed so this has no use to me.
As said in the doc-string from pyautogui.keyDown():
Performs a keyboard key press without the release. This will put that
key in a held down state.
NOTE: For some reason, this does not seem to cause key repeats like would
happen if a keyboard key was held down on a text field.
You need a different approach - you can may use pygame - with this
Or, if you want to stay with pyautogui you can try something like this:
def hold_W (hold_time):
import time, pyautogui
start = time.time()
while time.time() - start < hold_time:
pyautogui.press('w')
with pyautogui.hold(key):
pyautogui.sleep(hold)
This will do the trick without making your own function.

python3: how to use the press Ctrl+X (cut) and Ctrl+V using pynput?

following pynput documentation I tried this to "cut":
1: select some text in an editor
2: run this_code.py using a shortcut (without leaving the active windows)
from pynput.keyboard import Key, Controller
keyboard = Controller()
with keyboard.pressed(Key.ctrl):
keyboard.press('x')
keyboard.release('x')
The python console open actually print: ^X. The combination of keys are right but it doesn't do what it's suppose to do: cut the selected text store it in the clipboard. (I'm not interested to just store the clipboard content in a variable, I want a Ctrl+C)
I guess this answser will also solve the remaining part: Ctrl+V (to past some data which will be first inserted in the clipboard)
I took 3 things into consideration:
since I am on a mac, the combination is Command+X instead of Ctrl+X
I can only make it work if I use keyboard.press (pressed doesn't work for me, don't know why)
For the special keys, I have to use their Key.value (so, Key.ctrl becomes Key.ctrl.value; Key.Shift becomes Key.Shift.value...)
In the end, this worked for me:
# I tested this code as it is in Mac OS
from pynput.keyboard import Key, Controller
keyboard = Controller()
# keyboard.press(Key.ctrl.value) #this would be for your key combination
keyboard.press(Key.cmd.value)
keyboard.press('x')
keyboard.release('x')
# keyboard.release(Key.ctrl.value) #this would be for your key combination
keyboard.release(Key.cmd.value)
Even though this question is a bit old, I was having this same problem and found a solution that worked for me. Might come in handy for someone in the future.
I think you should use keyboard.type(msg) instead of keyboard.press(key)
I tried it on my side, and I think the problem is that you are using Key.ctrl as the argument for keyboard.pressed() on line 3.
Instead of that you should be using Key.ctrl.value.
So, the correct version of the code you have given would be:
from pynput.keyboard import Key, Controller
keyboard = Controller()
with keyboard.pressed(Key.ctrl.value):
keyboard.press('x')
keyboard.release('x')
First install pyautogui with:
pip install pyautogui
#Then in your code write:
import pyautogui
pyautogui.hotkey('ctrl', 'v')

Categories