5

Hello I'm trying to simulate a mouse click while holding the SHIFT key. I have been trying to do this with the pynput module.

This is my code so far:

from pynput.keyboard import Key
from pynput.keyboard import Controller as Cont
from pynput.mouse import Button, Controller
import time

mouse = Controller()
keyboard = Cont()

with keyboard.pressed(Key.shift):
    mouse.position = (1892, 838)
    mouse.click(Button.left)

I know the code for holding the shift key is working (If I try to press the "a" button in the code I see an "A"). Also I know the mouse click is working. However, together it does not work.


Also I tried another code from a StackOverflow post: Pyautogui - Need to hold shift and click

I tried the following code from it:

import pyautogui

pyautogui.keyDown('shift')
pyautogui.click()
pyautogui.keyUp('shift')

This worked for a minute then it stopped working! Very strange. It fails like 9 out of 10 times.

buræquete
  • 14,226
  • 4
  • 44
  • 89
teller.py3
  • 822
  • 8
  • 22
  • Try `mouse.press(Button.left)` and then `mouse.release(Button.left)`. Just a guess though, based on how Windows events usually work. – Blorgbeard Jun 06 '19 at 00:10
  • 1
    Already tried that. It did not work. Thanks for thinking with me though! – teller.py3 Jun 06 '19 at 00:11
  • @Blorgbeard yes – teller.py3 Jun 06 '19 at 00:12
  • It works then it does not work. It works like once in a very while. – teller.py3 Jun 09 '19 at 09:27
  • Hmm, seems to work for me. How do you test it? – CristiFati Jun 09 '19 at 18:22
  • @finefoot i followed your advice and put a time.sleep(10) between the two lines. It did not fix the problem – teller.py3 Jun 09 '19 at 20:28
  • @CristiFati I run my python scripts on sublime text 3. Or did you mean something else? – teller.py3 Jun 09 '19 at 20:29
  • @finefoot i am going to look in to that. I have a feeling you might be right – teller.py3 Jun 09 '19 at 21:09
  • 1
    @finefoot you were right. The target was the problem. Somehow it only works if I click on the target before starting to use the shift + click. I did not think that was needed. But it seems to be that case. If you make an answer I will accept it. I feel like you deserve that since you helped solving my problem! – teller.py3 Jun 09 '19 at 23:36
  • The question is lacking in detail it seems, because it is working on everyone else's environments. It works 100% on my environment as well, is it due to the focus was not on whatever the target you have and that app/browser whatever does not recognize the click without a focus on it? If so can you elaborate the target window? – buræquete Jun 14 '19 at 03:49
  • Can you please select the correct answer as the solution? You might confuse future people looking for a similar issue... The current accepted one again wouldn't work without a focus... – buræquete Jul 17 '19 at 09:07

3 Answers3

0

The script works as intended, but it seems the target on which you are trying to apply Shift + Left-Click is not accepting such inputs while its window on Windows GUI is not in focus. That is why it works when you include a Left-Click before the Shift + Left-Click, because that first click puts the target window (whatever program/app it is) in focus, then the already working but ignored Shift + Left-Click is also accepted by the target

buræquete
  • 14,226
  • 4
  • 44
  • 89
0

You should add a timer to it most likely will work.

import pyautogui
import time

#cordinates
cordinates = 100,100
pyautogui.keyDown('shift')
time.sleep(0.15)
pyautogui.click(cordinates)
time.sleep(0.15)
pyautogui.keyUp('shift')
-1

Well a workaround i suggest is creating an event listener like this :

from pynput.keyboard import Key, Listener

def on_press(key):
    print('{0} pressed'.format(
        key))

def on_release(key):
    print('{0} release'.format(
        key))
    if key == Key.esc:
        # Stop listener
        return False

enter code hereCollect events until released
with Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()
jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
Azer Gorai
  • 37
  • 1
  • 8