pynput - 导入键盘和鼠标
pynput - Importing keyboard and mouse
我在从 pynput 库导入一些东西时遇到了一些问题。
在我的代码中,我想使用 python 库 (pynput) 在鼠标和键盘上执行一些操作。当我只导入键盘或鼠标时,它可以工作,但是当同时导入两者时,它会给我一些错误。
这是我的代码:
from pynput.mouse import Button, Controller
from pynput.keyboard import Key, Controller
from time import sleep
mouse = Controller()
keyboard = Controller()
rock1x = 691
rock1y = 466
rock2x = 548
rock2y = 350
rock3x = 687
rock3y = 234
while (1):
drop1x = 1183
drop1y = 325
drop2x = 1220
drop2y = 325
drop3x = 1263
drop3y = 325
drop4x = 1303
drop4y = 325
for i in range(8):
sleep(2.5)
mouse.position = (rock2x,rock2y)
sleep(0.3)
mouse.press(Button.left)
mouse.release(Button.left)
sleep(2.1)
mouse.position = (rock3x,rock3y)
sleep(0.3)
mouse.press(Button.left)
mouse.release(Button.left)
sleep(2.1)
mouse.position = (rock1x,rock1y)
sleep(0.2)
mouse.press(Button.left)
mouse.release(Button.left)
for i in range(3):
keyboard.press(Key.shift)
for x in range(6):
mouse.position = (drop1x,drop1y)
sleep(0.3)
mouse.press(Button.left)
mouse.release(Button.left)
drop1y=drop1y+35
for x in range(6):
mouse.position = (drop2x,drop2y)
sleep(0.3)
mouse.press(Button.left)
mouse.release(Button.left)
drop2y=drop2y+35
for x in range(6):
mouse.position = (drop3x,drop3y)
sleep(0.3)
mouse.press(Button.left)
mouse.release(Button.left)
drop3y=drop3y+35
for x in range(6):
mouse.position = (drop4x,drop4y)
sleep(0.3)
mouse.press(Button.left)
mouse.release(Button.left)
drop4y=drop4y+35
keyboard.release(Key.shift)
sleep(3)
当我运行这段代码时出现以下错误:
Traceback (most recent call last):
File "mining.py", line 29, in <module>
mouse.press(Button.left)
File "/home/filipe/.local/lib/python2.7/site-packages/pynput/keyboard/_base.py", line 366, in press
if resolved.is_dead:
AttributeError: 'NoneType' object has no attribute 'is_dead'
但是当我评论第二行时:
from pynput.keyboard import Key, Controller
代码 运行s 直到:
keyboard.press(Key.shift)
并给我错误:
Traceback (most recent call last):
File "mining.py", line 48, in <module>
keyboard.press(Key.shift)
NameError: name 'Key' is not defined
我应该如何从pynput库导入才能使用鼠标和键盘?
我认为问题在于您导入了两个不同的 Controller
。
第二个 (pynput.keyboard.Controller
) 覆盖第一个,因为它是最后一个定义的。因此,您的变量 mouse
实际上是一个 pynput.keyboard.Controller
对象,而不是您预期的 pynput.mouse.Controller
对象。
当您调用 mouse.press(Button.left)
时发生错误,因为 Keyboard
对象正在尝试按 Button
,但它不能这样做(它只能按 Key
s ).
要解决此问题,请使用 import/as
导入模块 "generally" 而不是使用 from/import
:
导入其中的 "specific" 部分
import pynput.mouse as ms
import pynput.keyboard as kb
这样,您就可以区分两个控制器:
mouse = ms.Controller()
keyboard = kb.Controller()
希望这对您有所帮助 – 请回复任何反馈!
我在从 pynput 库导入一些东西时遇到了一些问题。
在我的代码中,我想使用 python 库 (pynput) 在鼠标和键盘上执行一些操作。当我只导入键盘或鼠标时,它可以工作,但是当同时导入两者时,它会给我一些错误。
这是我的代码:
from pynput.mouse import Button, Controller
from pynput.keyboard import Key, Controller
from time import sleep
mouse = Controller()
keyboard = Controller()
rock1x = 691
rock1y = 466
rock2x = 548
rock2y = 350
rock3x = 687
rock3y = 234
while (1):
drop1x = 1183
drop1y = 325
drop2x = 1220
drop2y = 325
drop3x = 1263
drop3y = 325
drop4x = 1303
drop4y = 325
for i in range(8):
sleep(2.5)
mouse.position = (rock2x,rock2y)
sleep(0.3)
mouse.press(Button.left)
mouse.release(Button.left)
sleep(2.1)
mouse.position = (rock3x,rock3y)
sleep(0.3)
mouse.press(Button.left)
mouse.release(Button.left)
sleep(2.1)
mouse.position = (rock1x,rock1y)
sleep(0.2)
mouse.press(Button.left)
mouse.release(Button.left)
for i in range(3):
keyboard.press(Key.shift)
for x in range(6):
mouse.position = (drop1x,drop1y)
sleep(0.3)
mouse.press(Button.left)
mouse.release(Button.left)
drop1y=drop1y+35
for x in range(6):
mouse.position = (drop2x,drop2y)
sleep(0.3)
mouse.press(Button.left)
mouse.release(Button.left)
drop2y=drop2y+35
for x in range(6):
mouse.position = (drop3x,drop3y)
sleep(0.3)
mouse.press(Button.left)
mouse.release(Button.left)
drop3y=drop3y+35
for x in range(6):
mouse.position = (drop4x,drop4y)
sleep(0.3)
mouse.press(Button.left)
mouse.release(Button.left)
drop4y=drop4y+35
keyboard.release(Key.shift)
sleep(3)
当我运行这段代码时出现以下错误:
Traceback (most recent call last):
File "mining.py", line 29, in <module>
mouse.press(Button.left)
File "/home/filipe/.local/lib/python2.7/site-packages/pynput/keyboard/_base.py", line 366, in press
if resolved.is_dead:
AttributeError: 'NoneType' object has no attribute 'is_dead'
但是当我评论第二行时:
from pynput.keyboard import Key, Controller
代码 运行s 直到:
keyboard.press(Key.shift)
并给我错误:
Traceback (most recent call last):
File "mining.py", line 48, in <module>
keyboard.press(Key.shift)
NameError: name 'Key' is not defined
我应该如何从pynput库导入才能使用鼠标和键盘?
我认为问题在于您导入了两个不同的 Controller
。
第二个 (pynput.keyboard.Controller
) 覆盖第一个,因为它是最后一个定义的。因此,您的变量 mouse
实际上是一个 pynput.keyboard.Controller
对象,而不是您预期的 pynput.mouse.Controller
对象。
当您调用 mouse.press(Button.left)
时发生错误,因为 Keyboard
对象正在尝试按 Button
,但它不能这样做(它只能按 Key
s ).
要解决此问题,请使用 import/as
导入模块 "generally" 而不是使用 from/import
:
import pynput.mouse as ms
import pynput.keyboard as kb
这样,您就可以区分两个控制器:
mouse = ms.Controller()
keyboard = kb.Controller()
希望这对您有所帮助 – 请回复任何反馈!