Skip to content

Instantly share code, notes, and snippets.

@kurosuke
Created August 12, 2024 05:24
Show Gist options
  • Select an option

  • Save kurosuke/a74cba437480502efb15e69bee8c8876 to your computer and use it in GitHub Desktop.

Select an option

Save kurosuke/a74cba437480502efb15e69bee8c8876 to your computer and use it in GitHub Desktop.
import evdev
from evdev import categorize, ecodes
import sys
def main():
# 利用可能なデバイスを一覧表示
devices = [evdev.InputDevice(path) for path in evdev.list_devices()]
if not devices:
print("利用可能なデバイスが見つかりません。")
sys.exit(1)
print("利用可能なデバイス:")
for i, device in enumerate(devices):
print(f"{i}: {device.path}, {device.name}, {device.phys}")
# ユーザーにデバイスを選択させる
device_index = int(input("使用するデバイスの番号を入力してください: "))
device = devices[device_index]
print(f"選択されたデバイス: {device.path}, {device.name}, {device.phys}")
print("デバイスの機能:")
for code, type_list in device.capabilities(verbose=True).items():
print("-"*80)
print(f" {code}:")
for t in type_list:
if isinstance(t, tuple):
print(f" {t[0]}, {t[1]}")
else:
print(f" {t}")
print("-"*80)
print(f"デバイス {device.name} からの入力を監視しています...")
alreays = []
try:
for event in device.read_loop():
if event.type == ecodes.EV_KEY:
key_event = categorize(event)
if key_event.keystate == key_event.key_up:
print(f"ボタン {key_event.keycode} が押されました")
elif event.type == ecodes.EV_ABS:
absevent = categorize(event)
if ecodes.ABS[absevent.event.code] in alreays:
continue
alreays.append(ecodes.ABS[absevent.event.code])
print(f"軸 {ecodes.ABS[absevent.event.code]} の値: {absevent.event.value}")
except IOError as e:
print(f"デバイスからの読み取り中にエラーが発生しました: {e}")
except KeyboardInterrupt:
print("プログラムが中断されました")
finally:
device.close()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment