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
# File 'lib/keystroker.rb', line 22

def initialize(kbml='<kbml/>', debug: false)
  
  @debug = debug
  
  if kbml then
    @doc = Rexle.new(RXFHelper.read(kbml).first)
  end
end

Instance Method Details

#parse_hg0(s) ⇒ Object



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/keystroker.rb', line 31

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

#to_au3Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/keystroker.rb', line 81

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_hg0Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/keystroker.rb', line 115

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



142
143
144
# File 'lib/keystroker.rb', line 142

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

#to_vbsObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/keystroker.rb', line 146

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