-2

It just doesnt work i get an error saying

AttributeError: 'Controller' object has no attribute 'is_pressed'

Code:

from pynput.mouse import Button, Controller
from pynput.keyboard import Key, Controller
from pynput.keyboard import Controller
from pynput import mouse
from pynput import keyboard

keyboard = Controller()

while True:
    if keyboard.is_pressed('u'):
        keyboard.press('w')
        keyboard.release('w')
       

Can someone help me out!

furas
  • 134,197
  • 12
  • 106
  • 148
Lagis
  • 7
  • 1
  • 7
  • `pynput` has special class `Listener` to catch pressed keys - and it doesn't need `while True`. `Controller` is only to send new keys. Why do you thing that there has to be `is_pressed` ? Did you check [documentation](https://pythonhosted.org/pynput/keyboard.html) ? – furas Jul 30 '20 at 12:54
  • Why did you think that the `is_pressed` attribute should exist? – mkrieger1 Jul 30 '20 at 12:58

2 Answers2

1

pynput has special class Listener to catch pressed keys. It runs in thread and it doesn' need while True so it doesn't block main code.

Press ESC to stop it.

from pynput.keyboard import Key, Controller, Listener

def on_press(key):
    print('{} pressed'.format(key))
    #print(dir(key))

    try:
        if key.char == 'u':
            keyboard.press('w')
            keyboard.release('w')
    except Exception as ex:
        print(ex)
    
def on_release(key):
    print('{} release'.format(key))

    if key == Key.esc:
        # Stop listener
        return False

# --- main ---

keyboard = Controller()

listener = Listener(on_press=on_press, on_release=on_release)

listener.start()

# ... other code ...

listener.join()

BTW:

If you want global macros/hotkeys in system and use Linux then you could use special program AutoKey for this. It is created with Python and it is has GUI to create macros/script/etc.

If you want global macros/hotkeys in system and use Windows you could rather use popular AutoHotKey

If you want to create macros/hotkeys in some GUI programs then you should use GUI functions for this.

furas
  • 134,197
  • 12
  • 106
  • 148
0

View the documentation, here's a snippet from it below.

Global hotkeys

A common use case for keyboard monitors is reacting to global hotkeys. Since a listener does not maintain any state, hotkeys involving multiple keys must store this state somewhere.

pynput provides the class pynput.keyboard.HotKey for this purpose. It contains two methods to update the state, designed to be easily interoperable with a keyboard listener: pynput.keyboard.HotKey.press and pynput.keyboard.HotKey.release which can be directly passed as listener callbacks.

[...]

This will create a hotkey, and then use a listener to update its state. Once all the specified keys are pressed simultaneously, on_activate will be invoked.

Note that keys are passed through pynput.keyboard.Listener.canonical before being passed to the HotKey instance. This is to remove any modifier state from the key events, and to normalise modifiers with more than one physical button.

The method pynput.keyboard.HotKey.parse is a convenience function to transform shortcut strings to key collections. Please see its documentation for more information.

To register a number of global hotkeys, use the convenience class pynput.keyboard.GlobalHotKeys:

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
John Carr
  • 69
  • 3