Skip to content

Instantly share code, notes, and snippets.

@afjk afjk/autoInput.rb

Last active Dec 26, 2015
Embed
What would you like to do?
require 'open3'
#-------------------------------------------------------------------------------
LOG_FILE = 'inputlog.log'
#-------------------------------------------------------------------------------
# Androidでの入力のログファイルへの出力
#-------------------------------------------------------------------------------
def recordToLogFile
Open3.popen3('adb shell') do |stdin,stdout,stderr,thread|
stdin.puts "getevent /dev/input/event0"
t = Time.now
f = File.open(LOG_FILE,'w')
loop{
line = stdout.readline
buff = line.split(' ')
if( buff.size == 3)
now = Time.now
d = now - t
t = now
puts "time diff:" + d.to_s
kind = buff[0].hex
key = buff[1].hex
value = buff[2].hex
puts kind.to_s + " " + key.to_s + " " + value.to_s
f.write(d.to_s + ","+ kind.to_s + " " + key.to_s + " " + value.to_s + "\n")
end
}
end
end
#-------------------------------------------------------------------------------
# ログファイルを読み込みAndroidへ自動入力
#-------------------------------------------------------------------------------
def playFromLogFile( inLoop=false )
loop{
Open3.popen3('adb shell') do |stdin,stdout,stderr,thread|
f = open(LOG_FILE)
f.each {|line|
buff = line.strip.split(',')
if buff.length == 2
wait = buff[0].to_f
sleep( wait )
puts "sendevent /dev/input/event0 " + buff[1]
stdin.puts( "sendevent /dev/input/event0 " + buff[1] )
end
}
f.close
stdin.puts( "exit");#adb shellを終了させる
end
puts( "end" )
if( !inLoop ) then
break;
end
sleep(5)
}
end
#-------------------------------------------------------------------------------
if ARGV[0] == 'p' then
inLoop = false;
if ARGV[1] == 'l' then
inLoop = true;
end
playFromLogFile(inLoop)
elsif ARGV[0] == 'r' then
recordToLogFile
else
puts "ruby autoInput.rb [r|p] [l]"
puts " r:record input to " + LOG_FILE
puts " p:play input from " + LOG_FILE
puts " l:play in loop "
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.