Last active
December 28, 2021 03:08
-
-
Save tmtm/57735a00ce75e0d0478e39284803f357 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
# キーイベントを表示する | |
EV_KEY = 1 | |
EVIOCGRAB = 1074021776 | |
key_code = {} | |
IO.readlines('/usr/include/linux/input-event-codes.h').each do |line| | |
if line.sub(/\/\*.*/, '') =~ /#define\s(KEY_\w+)+\s+(.+)(\/\*.*)?$/ | |
key, code = $1, $2 | |
eval("#{key} = #{code}") | |
key_code[key.sub(/\AKEY_/, '')] = eval(key) | |
end | |
end | |
code_key = key_code.invert | |
# struct input_event { | |
# struct timeval time; | |
# __u16 type; | |
# __u16 code; | |
# __s32 value; | |
# }; | |
# // sizeof(struct input_event) == 24 | |
# 引数は '/dev/input/event3' とか | |
f = File.open(ARGV[0]) | |
# f.ioctl(EVIOCGRAB, 1) # これを活かすとキーイベントがアプリに渡らなくなる | |
while true | |
raw = f.sysread(24) | |
_time, type, code, value = raw.unpack('a16SSl') # time は無視 | |
if type == EV_KEY | |
key = code_key[code] | |
event = value == 1 ? "press" : value == 0 ? "release" : nil | |
puts "#{key} #{event}" if event | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment