Created
September 8, 2024 22:08
-
-
Save shimarin/0acea55ab4eee406cc7b29c44b32b4fe to your computer and use it in GitHub Desktop.
コンソールでシフトキーが押されているかどうかを判定する関数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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