LoginSignup
1
4

More than 3 years have passed since last update.

pynputによるキー検出

Posted at

Pythonには、多くの機能を備えた pynput モジュールがあります。
今回は、pynputを使ってキー検出をしていきます。

開発環境

MacOS Catalina
Python 3.8.2
opencv-python 4.2.0.34

キー検出

早速やっていきたいと思います!


from pynput.keyboard import Key, Listener

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

def on_release(key):
    print('{0} release'.format(
        key))
    if key == Key.esc:
        # Stop listener
        return False

# Collect events until released
with Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()

このプログラムは、キーを押した時にそのキーが何かをprintしてくれるプログラムです。
「def on_press(key)」
このコードは( )の中のパラメーターによってキーが押された時に実行するようになっています。

参考

今回は短いですがこれで終わります。
https://www.it-swarm.dev/ja/python/python

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4