1

Какую библиотеку на Python можно использовать для отслеживания нажатия клавиш на клавиатуре и мыши?

1
  • Хотелось бы и на клавиатуре, и на мыши.
    – ZeN
    11 янв 2021 в 21:17

1 ответ 1

3

Для клавиатуры — keyboard

Пример с PyPI:

import keyboard

keyboard.press_and_release('shift+s, space')

keyboard.write('The quick brown fox jumps over the lazy dog.')

keyboard.add_hotkey('ctrl+shift+a', print, args=('triggered', 'hotkey'))

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey('page up, page down', lambda: keyboard.write('foobar'))

# Blocks until you press esc.
keyboard.wait('esc')

# Record events until 'esc' is pressed.
recorded = keyboard.record(until='esc')
# Then replay back at three times the speed.
keyboard.play(recorded, speed_factor=3)

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation('@@', '[email protected]')

# Block forever, like `while True`.
keyboard.wait()

Для мыши — pyautogui

Можно и для клавиатуры тоже

Отслеживание позиции мыши, пример:

import pyautogui, sys
print('Press Ctrl-C to quit.')
try:
    while True:
        x, y = pyautogui.position()
        positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
        print(positionStr, end='')
        print('\b' * len(positionStr), end='', flush=True)
except KeyboardInterrupt:
    print('\n')
3
  • а если мне нужно отслеживать не позицию мыши, а именно нажатия? На левую/правую/колесико/боковые кнопки мыши
    – ZeN
    11 янв 2021 в 21:23
  • @ZeN В доках вроде есть. ;) 11 янв 2021 в 21:23
  • @ZeN + pyHook ;) 11 янв 2021 в 21:24

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.