Skip to content

Instantly share code, notes, and snippets.

@tmtm
Last active December 28, 2021 01:56
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/e1d3955ec68787d29dff2d8d3e7394c8 to your computer and use it in GitHub Desktop.
Save tmtm/e1d3955ec68787d29dff2d8d3e7394c8 to your computer and use it in GitHub Desktop.
自動的に 0〜9 キーを入力する
# 自動的に 0〜9 キーを入力する
UI_SET_EVBIT = 1074025828
UI_SET_KEYBIT = 1074025829
UI_DEV_SETUP = 1079792899
UI_DEV_CREATE = 21761
EV_KEY = 1
EV_SYN = 0
SYN_REPORT = 0
# struct input_id {
# __u16 bustype;
# __u16 vendor;
# __u16 product;
# __u16 version;
# };
#
# struct uinput_setup {
# struct input_id id;
# char name[UINPUT_MAX_NAME_SIZE]; // 80
# __u32 ff_effects_max;
# };
# // sizeof(struct uinput_setup) == 92
#
# struct input_event {
# struct timeval time;
# __u16 type;
# __u16 code;
# __s32 value;
# };
# // sizeof(struct input_event) == 24
f = File.open("/dev/uinput", "w")
f.ioctl(UI_SET_EVBIT, EV_KEY)
0x300.times{|k| f.ioctl(UI_SET_KEYBIT, k)}
bustype = 0x03 # BUS_USB
vendor = 0x1234 # テキトー
product = 0x5678 # テキトー
version = 1 # テキトー
name = 'Example Device'
ff_efects_max = 0
setup = [bustype, vendor, product, version, name, ff_efects_max].pack("SSSSZ80L") # uinput_setup
f.ioctl(UI_DEV_SETUP, setup)
f.ioctl(UI_DEV_CREATE)
loop do
(2..11).each do |k|
sleep 1
f.syswrite(['', EV_KEY, k, 1].pack('a16SSl')) # input_event press
f.syswrite(['', EV_SYN, SYN_REPORT, 0].pack('a16SSl')) # input_event
f.syswrite(['', EV_KEY, k, 0].pack('a16SSl')) # input_event release
f.syswrite(['', EV_SYN, SYN_REPORT, 0].pack('a16SSl')) # input_event
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment