LoginSignup
10
10

More than 5 years have passed since last update.

Pythonでキーが押されているか判定する

Posted at

はじめに

 滅多に無いと思うけど,特定のキーが今押されているかを判定したい時に使う.自分用のメモ.

方法

import ctypes

def isPressed(key):
    return(bool(ctypes.windll.user32.GetAsyncKeyState(key)&0x8000))

ESC=0x1B
while True:
    print(isPressed(ESC))

Enterを押さなくてもリアルタイムに取得できるし,ウィンドウがフォーカスされていなくても判定できる,と思う.

10
10
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
10
10