Skip to content

Instantly share code, notes, and snippets.

@shimarin
Created September 8, 2024 22:08
Show Gist options
  • Save shimarin/0acea55ab4eee406cc7b29c44b32b4fe to your computer and use it in GitHub Desktop.
Save shimarin/0acea55ab4eee406cc7b29c44b32b4fe to your computer and use it in GitHub Desktop.
コンソールでシフトキーが押されているかどうかを判定する関数
def is_shift_key_being_pressed():
TIOCLINUX = 0x541C # TIOCLINUXの値はLinuxのカーネルヘッダファイルから取得
shift_state = 6 # 変数の初期値
# 変数をバイトオブジェクトに変換
shift_state_buf = struct.pack('B', shift_state)
try:
# ioctlシステムコールを使用してシフト状態を取得
shift_state_buf = fcntl.ioctl(sys.stdin.fileno(), TIOCLINUX, shift_state_buf)
# 取得した結果をアンパックして表示
shift_state = struct.unpack('B', shift_state_buf)[0]
if shift_state != 0: return True
except IOError as e:
logging.warning(f"ioctl TIOCLINUX 6 (get shift state) failed: {e}")
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment