在Windows中按住SHIFT键的同时如何模拟鼠标单击? [英] How to simulate a mouse click while holding the SHIFT key in Windows?
问题描述
您好,我试图按住 SHIFT 键的同时模拟鼠标单击.我一直在尝试使用pynput
模块来实现这一点.
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)
我知道按住Shift键的代码可以正常工作(如果我尝试按 代码中的"a"按钮,我看到一个"A").我也知道鼠标单击是有效的.但是,总的来说,这是行不通的.
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.
我还尝试了StackOverflow帖子中的另一个代码: Pyautogui-需要按住并单击
Also I tried another code from a StackOverflow post: Pyautogui - Need to hold shift and click
我尝试了以下代码:
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.
推荐答案
您应该向其中添加一个计时器,该计时器很可能会起作用.
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')
这篇关于在Windows中按住SHIFT键的同时如何模拟鼠标单击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!