Class: Xkeyrap::Command

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device, config) ⇒ Command

Returns a new instance of Command.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/xkeyrap/command.rb', line 11

def initialize(device, config)
  self.modifier_key = nil
  self.output_device = device
  unless config
    self.config = {
      global: {
        KEY_CAPSLOCK: :KEY_LEFTCTRL,
        KEY_LEFTCTRL: :KEY_CAPSLOCK,
        KEY_LEFTALT:  :KEY_LEFTMETA,
        KEY_LEFTMETA: :KEY_LEFTALT
      },
      "Google-chrome": {
        KEY_LEFTALT: :KEY_LEFTCTRL,
        KEY_CAPSLOCK: :KEY_LEFTMETA,
        KEY_LEFTMETA: :KEY_LEFTALT  
      }
    }
  end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/xkeyrap/command.rb', line 9

def config
  @config
end

#modifier_keyObject

Returns the value of attribute modifier_key.



7
8
9
# File 'lib/xkeyrap/command.rb', line 7

def modifier_key
  @modifier_key
end

#output_deviceObject

Returns the value of attribute output_device.



8
9
10
# File 'lib/xkeyrap/command.rb', line 8

def output_device
  @output_device
end

Instance Method Details

#output_combine(modifier_key, key) ⇒ Object



62
63
64
65
66
67
# File 'lib/xkeyrap/command.rb', line 62

def output_combine(modifier_key, key)
  output_event(self.modifier_key, 1)
  output_event(key, state)
  output_event(self.modifier_key, 0)
  self.modifier_key = nil
end

#output_event(key, state, wm_class_name) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/xkeyrap/command.rb', line 69

def output_event(key, state, wm_class_name)
  sub_json = self.config[wm_class_name.to_sym] || self.config[:global]
  mapped_key = sub_json[key] || self.config[:global][key] || key
  puts "#{wm_class_name} | #{state} | origin: #{key} | mapped: #{mapped_key}"
  self.output_device.send_event(:EV_KEY, mapped_key, state)
  self.output_device.send_event(:EV_SYN, :SYN_REPORT)
end

#receive(state, key, wm_class_name = "global") ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/xkeyrap/command.rb', line 31

def receive(state, key, wm_class_name = "global")
  if Key.is_modifier_key?(key)
    if state == 1 || state == 2
      self.modifier_key = key # and do nothing to output
    else # state = 0
      self.modifier_key = nil
    end
  else
    if self.modifier_key
      transport(self.modifier_key, key, wm_class_name)
    else
      output_event(key, state, wm_class_name)
    end
  end
end

#transport(modifier_key, key, wm_class_name) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/xkeyrap/command.rb', line 47

def transport(modifier_key, key, wm_class_name)
  if wm_class_name == "Google-chrome"
    if modifier_key == :KEY_LEFTMETA && key == :KEY_A
      puts "win+a"
      output_event(:KEY_HOME, 1, wm_class_name)
    end
    if modifier_key == :KEY_LEFTMETA && key == :KEY_E
      puts "win+e"
      output_event(:KEY_END, 1, wm_class_name)
    end      
  else
    output_combine(modifier_key, key)
  end
end