Class: XInputWrapper

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

Instance Method Summary collapse

Constructor Details

#initialize(device: nil, verbose: true, lookup: {}, debug: false) ⇒ XInputWrapper

device list:

3 = Virtual core keyboard
4 = Virtual core XTEST pointer (active when using VNC)
5 = Virtual core XTEST keyboard (active when using VNC)
10 = USB Optical Mouse (locally attached)
11 = Microsoft Wired Keyboard 600 (locally attached)


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
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
95
96
# File 'lib/xinput_wrapper.rb', line 29

def initialize(device: nil, verbose: true, lookup: {}, debug: false )

  # defaults to QWERTY keyboard layout
  @modifiers = {
    62 => :shift,     # right control
    37 => :ctrl,   # left control 
    50 => :shift,     # left shift
    64 => :alt,       # alt shift
    92 => :alt,       # right alt  
    105 => :ctrl,  # right control     
    133 => :super,    # left super (windows key)
    134 => :super     # right super (windows key)
  }
  @lookup = {
    10=>:"1", 11=>:"2", 12=>:"3", 13=>:"4", 14=>:"5", 15=>:"6", 16=>:"7", 
    17=>:"8", 18=>:"9", 19=>:"0", 20=>:-, 21=>:"=", 22=>:backspace, 
    23=>:tab, 24=>:q, 25=>:w, 26=>:e, 27=>:r, 28=>:t, 29=>:y, 30=>:u, 
    31=>:i, 32=>:o, 33=>:p, 34=>:"[", 35=>:"]", 36=>:enter, 38=>:a, 39=>:s, 
    40=>:d, 41=>:f, 42=>:g, 43=>:h, 44=>:j, 45=>:k, 46=>:l, 47=>:";", 
    48=>:"'", 49=>nil, 52=>:z, 53=>:x, 54=>:c, 55=>:v, 56=>:b, 57=>:n, 
    58=>:m, 59=>:",", 60=>:".", 61=>:/,  65=>:space,
    9 => :esc,
    66 => :capslock,
    67 => :f1,
    68 => :f2,
    69 => :f3,
    70 => :f4,
    71 => :f5,
    72 => :f6,
    73 => :f7,
    74 => :f8,
    75 => :f9,
    76 => :f10,
    77 => :numlock,
    78 => :scrolllock,
    95 => :f11,
    96 => :f12,
    107 => :sysrq, # print_screen
    110 => :home,
    111 => :up, # arrow keys
    112 => :pageup,
    113 => :left, # arrow keys
    114 => :right, # arrow keys
    115 => :end, 
    116 => :down, # arrow keys
    117 => :pagedown,
    121 => :mute,
    122 => :vol_down,
    123 => :vol_up,
    127 => :pause_break,
    135 => :submenu,
    148 => :calc,
    150 => :sleep,
    151 => :wakeup,
    163 => :email,
    166 => :go_back,
    167 => :go_forward,
    171 => :next_track,
    172 => :play_stop,
    173 => :prev_track,
    174 => :stop,
    179 => :music,
    180 => :browser
  }.merge(@modifiers).merge(lookup)
  
  @device, @verbose, @debug = device, verbose, debug
  
end

Instance Method Details

#listenObject



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
126
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
160
161
162
163
164
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
191
192
193
194
195
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/xinput_wrapper.rb', line 98

def listen()
  
  command = "xinput test-xi2 --root #{@device}"

  type = 0
  raw_keys = []
  t1 = Time.now
  lines = []

  IO.popen(command).each_line do |x|
 
    print "GOT ", x
    if x[/EVENT type \d \(Motion\)/] and (Time.now > (t1 + 0.06125)) then 

      type = x[/EVENT type (\d+)/,1].to_i
  
      r = lines.join[/^\s+root: (\d+\.\d{2}\/\d+\.\d{2})/,1]

      if r then
    
        x1, y1 = r.split('/').map(&:to_f) 
        puts "x1: %s y1: %s" % [x1, y1] if @debug
        on_mousemove(x1, y1)
        t1 = Time.now  
  
      end        

      lines = [x]                                                                

    elsif x[/EVENT type \d+ \(RawKey(?:Release|Press)\)/]

      type = x[/EVENT type (\d+)/,1].to_i

      lines = [x]

    elsif type == MOTION or type == RAWKEY_PRESS or type == RAWKEY_RELEASE

      lines << x
  
      if x == "\n" then
        case lines.first[/(?<=EVENT type )\d+/].to_i
        when RAWKEY_PRESS
    
          r = lines.join[/detail: (\d+)/,1]

          keycode = r.to_i if r

          type = lines.join[/EVENT type (\d+)/,1] .to_i
    
        when RAWKEY_RELEASE
    
          r = lines.join[/detail: (\d+)/,1]

          keycode = r.to_i if r

          type = lines.join[/EVENT type (\d+)/,1] .to_i          
    
        end    

  
      else
          next
      end
  
    else
      next
    end
  
    next unless keycode
    puts 'keycode: ' + keycode.inspect if @debug
        
    # type = 13 means a key has been pressed
    if type == RAWKEY_PRESS then
  
      if @modifiers.include? raw_keys.last or @modifiers.include? keycode then
        raw_keys << keycode
      end
  
      next if @modifiers.include? keycode

      puts 'raw_keys: ' + raw_keys.inspect if @debug

      if raw_keys.length <= 1 then
        puts 'keycode: ' + keycode.to_s if keycode > 0 and @verbose
        puts ('>keycode: ' + keycode.to_s).debug  if @debug
    
        key = @lookup[keycode]
        


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

        if key then
  
          puts key.to_s + ' key presssed' if @verbose
          name = "on_#{key}_key".to_sym
          method(name).call if self.protected_methods.include? name
          
          keystring = ((key.length > 1 or key == ' ') ? "{%s}" % key : key)    
          block_given? ? yield(keystring) : on_key_press(keystring, keycode)

        end        
  
      else

        keys = raw_keys.map {|kc| @lookup[kc] }
        puts ('keys: ' + keys.inspect) if @debug
  
        if block_given? then
          yield(format_key(keys.last, keys[0..-2]))
        else
          on_key_press(keys.last, keycode, keys[0..-2])    
        end
  
        raw_keys = []

      end
  


    # a key has been released
    elsif type == RAWKEY_RELEASE
  
      # here we are only looking to detect a 
      # single modifier key press and release
  
      key = @lookup[keycode]
  
      unless raw_keys.empty? then
        puts key.to_s + ' key presssed' 
  
        if block_given? then
  
          yield(format_key(key.to_s))
  
        else
          name = "on_#{key}_key".to_sym
          method(name).call if self.protected_methods.include? name
          on_key_press(key, keycode)
        end
      end
  
      index = raw_keys.rindex(keycode)
      raw_keys.delete_at index if index

    end    
  
  end
end