I have a problem with Pynput, whenever i use both the mouse and keyboard functions together the output responds with:

AttributeError: 'str' object has no attribute 'value'

I've tried tried to find a solution but nothing that i can find has worked. I've attached my script, the error only seems to happen when im using both the mouse and keyboard functions.

from pynput.keyboard import Key, Controller
from pynput.mouse import Button, Controller
mouse = Controller()
keyboard = Controller()

key1 = "s"
key2= "t"
key3 = "o"
key4 = "p"

mouse.position = (-1180, 153)
mouse.click(Button.left, 1)
keyboard.press(key1)
keyboard.release(key1)
keyboard.press(key2)
keyboard.release(key2)
keyboard.press(key3)
keyboard.release(key3)
keyboard.press(key4)
keyboard.release(key4)

Thank You - Connor

1

There are 1 best solutions below

0
On

As mentioned in the comments, you are importing two classes with the same name. Use an alias to differentiate them in the code.

This code works as expected:

from pynput.keyboard import Key, Controller
from pynput.mouse import Button, Controller as MController  # alias
mouse = MController()
keyboard = Controller()

key1 = "s"
key2= "t"
key3 = "o"
key4 = "p"

mouse.position = (-1180, 153)
mouse.click(Button.left, 1)
keyboard.press(key1)
keyboard.release(key1)
keyboard.press(key2)
keyboard.release(key2)
keyboard.press(key3)
keyboard.release(key3)
keyboard.press(key4)
keyboard.release(key4)