Class: Keystroker

Inherits:
Object
  • Object
show all
Defined in:
lib/keystroker.rb

Instance Method Summary collapse

Constructor Details

#initialize(kbml = '<kbml/>', debug: false) ⇒ Keystroker

Returns a new instance of Keystroker.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/keystroker.rb', line 22

def initialize(kbml='<kbml/>', debug: false)
  
  @debug = debug
  
  @doc = if kbml then
  
    s, _ = RXFHelper.read(kbml)
          
    head, body = s.lines[0].chomp, s.lines[1..-1].join
    puts 'head:' + head.inspect if @debug
    
    case head
    when /<\?kbml\?\>/
      parse_slim(body)
    when /<\?hg0\?\>/
      parse_hg0(body)
    else
      Rexle.new(s)
    end
    
  end
  
end

Instance Method Details

#parse_hg0(s) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/keystroker.rb', line 46

def parse_hg0(s)

  xml = RexleBuilder.new

  a3 = xml.kbml do

    a = s.gsub(/\s*(?=\{)|(?<=\})\s*/,'').scan(/\{[^\}]+\}|./)\
      .chunk {|x| x.length == 1}.map {|b, x| b ? x.join : x }.each do |x|

      if x.is_a? Array then

        x.each do |token|

          token[1..-2].split(/\s*[;,]\s*/).each do |instruction|

            puts ('instruction: ' + instruction.inspect).debug if @debug

            if instruction =~ /\*\s*\d+/ then

              key, n = instruction.split('*',2)
              #n.to_i.times {keypress(key, duration: duration) }
              xml.send(key.to_sym, {repeat: n})

            else

              keys = instruction.split('+')     

              if keys.length > 1 then
                name = keys[0..-2].join('_').to_sym
                xml.send(name, {key: keys.last})
              else
                xml.send(instruction.to_sym)
              end
            end              

          end
        end

      else
        xml.type x
      end
         
    end

  end

  @doc = Rexle.new(a3)

end

#parse_slim(s) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/keystroker.rb', line 96

def parse_slim(s)    
  
  puts 'inside parse_slim' if @debug
  
  a = s.strip.lines.map do |line|

    head, rawbody = line.chomp.split(/ +/,2)
    body = rawbody ? rawbody.strip : nil
    puts 'head: ' + head.inspect if @debug

    case head.to_sym
    when :activate
      [:window, {activate: body}]
    when :enter
      [:enter, {}]
    when :sleep
      [:sleep, {duration: body}]                
    when :tab
      [:tab, {times: body}]
    when :type
      [:type, {}, body]                
    end

  end
  
  rawxml = ['kbml', {}, '', *a]

  @doc = Rexle.new(rawxml)

end

#to_au3Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/keystroker.rb', line 127

def to_au3()

  a = []

  @doc.root.each_recursive do |x|

    next unless x

    if x.name == 'type' then
      a << ''
      a << %Q{Send("%s")} % x.text
    else

      modifiers = {ctrl: '^', shift: '+', alt: '!', win: '#'}

      instruction = if modifiers[x.name.to_sym] then
        modifiers[x.name.to_sym]
      else
        "{%s}" % x.name.upcase
      end

      if x.attributes[:key] then
        instruction += '' + x.attributes[:key] + ''
      end

      a << %Q{Send("%s")} % instruction
      a << '' if x.name == 'enter'
    end

  end
  
  puts a.join("\n")      
end

#to_docObject



161
162
163
# File 'lib/keystroker.rb', line 161

def to_doc()
  @doc
end

#to_hg0Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/keystroker.rb', line 165

def to_hg0()
  
  a = []

  @doc.root.each_recursive do |x|

    if @debug then
      puts ('x:' + x.inspect).debug
      puts ('x2: ' + x.next_sibling.inspect).debug
    end
    
    next unless x
    
    if x.name == 'type' then
      a << ' ' + x.text
    else
      instruction = x.name
      instruction += '+' + x.attributes[:key] if x.attributes[:key]
      a << ' {' + instruction + '}'
    end

  end
  
  a.join.lstrip
  
end

#to_kbml(options = {}) ⇒ Object



192
193
194
# File 'lib/keystroker.rb', line 192

def to_kbml(options={})
  @doc.xml(options)
end

#to_vbsObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/keystroker.rb', line 196

def to_vbs()
  
  a = []

  @doc.root.each_recursive do |x|

    next unless x

    if x.name == 'type' then
      a << ''
      a << %Q(WshShell.SendKeys "%s") % x.text
    else

      modifiers = {ctrl: '^', shift: '+', alt: '%'}

      instruction = if modifiers[x.name.to_sym] then
        modifiers[x.name.to_sym]
      else
        "{%s}" % x.name.upcase
      end

      if x.attributes[:key] then
        instruction += '{' + x.attributes[:key] + '}'
      end

      a << %Q(WshShell.SendKeys "%s") % instruction
      a << '' if x.name == 'enter'
    end

  end
  
  a.prepend('Set WshShell = WScript.CreateObject("WScript.Shell")' + "\n")
  puts a.join("\n")    
end