Skip to content Skip to sidebar Skip to footer

Pynput - Importing Keyboard And Mouse

I am having some trouble importing some things from pynput library. In my code I want use the a python library (pynput) to do some actions in the mouse and in keyboard. When I impo

Solution 1:

I think that the problem is that you are importing two different Controllers.

The second one (pynput.keyboard.Controller) overrides the first one since it is the last one defined. Therefore, your variable mouse is actually a pynput.keyboard.Controller object and not a pynput.mouse.Controller object like you expected.

The error happens when you call mouse.press(Button.left) because the Keyboard object is trying to press a Button, which it cannot do (it can only press Keys).

To fix this, import the modules "generally" using import/as instead of importing "specific" parts of them using from/import:

import pynput.mouseas ms
import pynput.keyboardas kb

This way, you can differentiate between the two controllers:

mouse    = ms.Controller()
keyboard = kb.Controller()

Hope this helps – please respond with any feedback!

Post a Comment for "Pynput - Importing Keyboard And Mouse"