Skip to content

Instantly share code, notes, and snippets.

@tmtm
Last active December 28, 2021 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmtm/57735a00ce75e0d0478e39284803f357 to your computer and use it in GitHub Desktop.
Save tmtm/57735a00ce75e0d0478e39284803f357 to your computer and use it in GitHub Desktop.
キーイベントを表示する
# キーイベントを表示する
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