JuniorPy
JuniorPy

Reputation: 61

How to fix "TypeError: 'KeyCode' object is not subscriptable"

I'm learning about pynput and make this simple code.

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

try:
    while True:
        mouse.press(Button.left)
        keyboard.press(Key.shift)
except KeyboardInterrupt:
    print("Interrupted")

And i don't know why this give me a:

TypeError: 'KeyCode' object is not subscriptable

Upvotes: 0

Views: 1473

Answers (1)

JuniorPy
JuniorPy

Reputation: 61

Ok.. now I have solution. If I want both keyboard controller and mouse I need to import like this

from pynput.keyboard import Key, Controller as KeyboardController
from pynput.mouse import Button, Controller as MouseController

and then use..

keyboard = KeyboardController()
mouse = MouseController()

Upvotes: 5

Related Questions