class HidG0

Attributes

duration[RW]

Public Class Methods

new(dev='/dev/hidg0', debug: false, humanspeed: true) click to toggle source
# File lib/hidg0.rb, line 311
def initialize(dev='/dev/hidg0', debug: false, humanspeed: true)
  @dev, @debug = dev, debug
  @duration = humanspeed ? 0.22 : 0
end

Public Instance Methods

keypress(raw_key, duration: 0) click to toggle source
# File lib/hidg0.rb, line 316
def keypress(raw_key, duration: 0)

  key = raw_key.strip.empty? ? raw_key : raw_key.strip
  keydown(key); sleep(duration); release_keys()

end
sendkeys(s, duration: @duration) click to toggle source
# File lib/hidg0.rb, line 323
def sendkeys(s, duration: @duration)
  
  # current keymapping is for en-gb
  
  # £ is "\u{00A3}" in unicode
  parse_keys(s).each do |x|
    
    puts ('x: ' + x.inspect).debug if @debug
    
    if x.length == 1 and x[0] != '{' then
      
      keypress x, duration: duration
      
    else
      
      # split by semicolon
      
      x[1..-2].split(/\s*[;,]\s*/).each do |instruction|
      
        if instruction =~ /\*\s*\d+/ then
          key, n = instruction.split('*',2)
          n.to_i.times {keypress(key, duration: duration) }
          next
        end
        
        keys = instruction.split('+')        
      

        puts ('keys: ' + keys.inspect).debug if @debug              
        
        if keys.length > 1 then
                      
          # e.g. keys #=> ['ctrl', 's']
        
          key = KEYS[keys.pop.to_sym]          
          modifier = keys.map {|x| MODIFIERS[x.to_sym]}.inject(:+)
          
          if @debug then
            puts ('key: ' + key.inspect).debug
            puts ('modifier: ' + modifier.inspect).debug
          end
          
          write_report(modifier.chr + NULL_CHAR + key.chr + NULL_CHAR*5)
          release_keys()
          
        else
             
          key = keys.first
          
          if key =~ /sleep/ then
            
            seconds = key[/(?<=sleep )\d+(?:\.\d+)?/] || '0.5'
            puts ('sleeping for ' + seconds + ' seconds').info if @debug
            sleep seconds.to_f
            
          else
          
            keypress key, duration: duration
          end
        end
      end
    end
  end
  
end

Protected Instance Methods

parse_keys(raw_s) click to toggle source
# File lib/hidg0.rb, line 391
def parse_keys(raw_s)
  
  s = raw_s.clone
  
  [["\u{00A3}","{shift+3}"],['"','{shift+2}']]\
      .map {|x,y| s.gsub!(x,y) }
  
  s.gsub(/\s*(?=\{)|(?<=\})\s*/,'').scan(/\{[^\}]+\}|./)
  
end

Private Instance Methods

keydown(key) click to toggle source
# File lib/hidg0.rb, line 404
def keydown(key)
  
  puts 'keydown | key: ' + key.inspect if @debug
  
  return write_report(8.chr + NULL_CHAR*7) if key.to_s =~ /^windows_key$/
  
  if KEYS[key.to_sym].is_a? Integer then 

    write_report(NULL_CHAR*2 + KEYS[key.to_sym].chr + NULL_CHAR*5)
    
  else 
    
    # the key can be reproduced by combining tke key press with the shift key
    puts ('KEYS[key.to_sym]: ' + KEYS[key.to_sym].inspect).debug if @debug
    write_report(MODIFIERS[:shift].chr + NULL_CHAR + \
                 KEYS[KEYS[key.to_sym]].chr + NULL_CHAR*5)      
    
  end    
  
end
release_keys() click to toggle source
# File lib/hidg0.rb, line 425
def release_keys()
  write_report(NULL_CHAR*8)
end
write_report(report) click to toggle source
# File lib/hidg0.rb, line 429
def write_report(report)
  open(@dev, 'wb+') {|f| f.write report } if @dev
end