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')
Related
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
Below is my code and .click function is clicking on a point where my mouse pointer is . But i need to locate something with a image and need to click that . Anyone have a solution for this ??
import pyautogui
import os
os.system(r'C:\Users\User\AppData\Roaming\Zoom\bin\Zoom.exe')
position = pyautogui.locateOnScreen(r"join_button.png")
pyautogui.click(position)
This answer might be late but I am pretty sure you have to use pyautogui.center(position) after locating on screen. So, the code would look something like this:
import pyautogui
import os
os.system(r'C:\Users\User\AppData\Roaming\Zoom\bin\Zoom.exe')
position = pyautogui.locateOnScreen(r"join_button.png")
center_pos = pyautogui.center(position)
pyautogui.click(center_pos)
My goal is to use custom hotkeys to execute different funcions in Python. I found Pynput by googling. Here is the official example from Pynput documentation (https://pynput.readthedocs.io/en/latest/keyboard.html#controlling-the-keyboard):
from pynput import keyboard
def on_activate():
print('Global hotkey activated!')
def for_canonical(f):
return lambda k: f(l.canonical(k))
hotkey = keyboard.HotKey(
keyboard.HotKey.parse('<ctrl>+<alt>+h'),
on_activate)
with keyboard.Listener(
on_press=for_canonical(hotkey.press),
on_release=for_canonical(hotkey.release)) as l:
l.join()
I only a few lines of pyautogui commands. So my code looks like this:
from pynput import keyboard
import pyautogui, time
def on_activate_h():
pyautogui.click(1000, 500)
pyautogui.click(1000, 600)
## time.sleep(0.5)
pyautogui.click(1000, 700)
pyautogui.click(1000, 800)
print('<ctrl>+<alt>+h pressed')
def on_activate_i():
print('<ctrl>+<alt>+i pressed')
with keyboard.GlobalHotKeys({
'<ctrl>+<alt>+h': on_activate_h,
'<ctrl>+<alt>+i': on_activate_i}) as h:
h.join()
When executing the code, some weird stuff happens. When pressing "Ctrl+Alt+h" for the first one or two times, it looks fine. Then I found that pressing "Ctrl+Alt" will execute the code in the on_activate_h() function too.
When I remove one line of the four pyautogui.click() lines, the problem seems gone. However, when pressing "Ctrl+Alt+h" twice in a row very quick, the problem comes back.
I tried to solve the problem by inserting the line "time.sleep(0.5)" to Line 7, but it doesn't work. The problem persists.
I'm a self-taught novice programmer who learnt some Python online at automatetheboringstuff.com. So the problem seems really weired to me. Could someone help me solve this? Or is there a better way to define different hotkeys to run different codes? Thanks in advance. I would be very grateful for any solutions.
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.
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')