-
Notifications
You must be signed in to change notification settings - Fork 1
/
keylist.rb
64 lines (56 loc) · 1.69 KB
/
keylist.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require './utils.rb'
include Utils
class KeyList
LIST_LENGTH = 10000
PROB_LOWER_BOUND = 0.01
PROB_UPPER_BOUND = 0.02
FELL_DIST_IMAGE = '133x25+643+432'
TIME_BETWEEN_BINS = 0.01
attr_reader :keylist, :time, :distance
attr_accessor :score
def initialize(keylist = [])
@keys = {'q'=>true,'w'=>true,'o'=>true,'p'=>true} #true == key is up
@keylist = keylist
@time = 0
@distance = 0.0
@score = 0
if @keylist.length < LIST_LENGTH then
self.build
end
end
def build
prng = Random.new
odds = prng.rand(PROB_LOWER_BOUND..PROB_UPPER_BOUND)
until @keylist.length == LIST_LENGTH do
bin_array = []
@keys.each do |key, state|
if rand < odds then
bin_array << key + (state ? 'd' : 'u')
@keys[key] = !state
end
end
@keylist << bin_array
end
end
def test
Utils.prepare_applet
browser_window_id = `xdotool search --name "#{Utils::WINDOW_NAME}"`.strip
@keylist.each_with_index do |keys, time|
keys.each do |key|
command = (key[1] == 'u' ? 'keyup' : 'keydown')
`xdotool #{command + ' --window ' + browser_window_id + ' --delay 0 ' + key[0]}`
end
pixel_color = `./grabpixel "#{Utils::WINDOW_NAME}" #{Utils::FELL_PIXEL}`.strip
if pixel_color == Utils::FELL_PIXEL_COLOR then
distance = `xdotool search -name "#{Utils::WINDOW_NAME}" windowactivate \
&& xwd -name "#{Utils::WINDOW_NAME}" -out "Distance.xwd" \
&& convert Distance.xwd -negate -crop '#{FELL_DIST_IMAGE}' Distance#{time}.pnm \
&& rm -f Distance.xwd && gocr -u "1" -C "--0-9.metrs " Distance#{time}.pnm`.gsub!(' ','').to_f
`rm Distance#{time}.pnm`
@distance, @time = distance, time
return distance, time
end
sleep(TIME_BETWEEN_BINS)
end
end
end