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, state, wm_class_name) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/xkeyrap/command.rb', line 76

def output_combine(modifier_key, key, state, wm_class_name)      
  puts "output combine: #{modifier_key}, #{key}, #{state}, #{wm_class_name}"
  # output_event(self.modifier_key, 1, wm_class_name)
  # output_event(key, state, wm_class_name)
  # output_event(self.modifier_key, 0, wm_class_name)      
  if state == 1
    self.output_device.send_event(:EV_KEY, self.modifier_key, 1)
    self.output_device.send_event(:EV_KEY, key, state)
    self.output_device.send_event(:EV_KEY, self.modifier_key, 0)
  end
  self.output_device.send_event(:EV_SYN, :SYN_REPORT)
  self.modifier_key = nil
end

#output_event(key, state, wm_class_name) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/xkeyrap/command.rb', line 90

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
46
47
48
# 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
      puts "set modifier key: #{key}"
      self.modifier_key = key # and do nothing to output
    else # state = 0
      puts "clear modifier key: #{self.modifier_key}"
      self.modifier_key = nil
    end
  else # normal key
    if self.modifier_key
      transport(self.modifier_key, key, state, wm_class_name)
    else
      puts "normal key: #{key}"
      output_event(key, state, wm_class_name)
    end
  end
end

#transport(modifier_key, key, state, wm_class_name) ⇒ Object



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
# File 'lib/xkeyrap/command.rb', line 50

def transport(modifier_key, key, state, wm_class_name)
  sub_json = self.config[wm_class_name.to_sym] || self.config[:global]
  mapped_modifier_key = sub_json[modifier_key] || self.config[:global][modifier_key] || modifier_key

  if wm_class_name == "Google-chrome"
    if mapped_modifier_key == :KEY_LEFTMETA
      mapped_key_config = {
        :KEY_A => :KEY_HOME,
        :KEY_E => :KEY_END,
        :KEY_B => :KEY_LEFT,
        :KEY_F => :KEY_RIGHT,
        :KEY_N => :KEY_DOWN,
        :KEY_P => :KEY_UP
      }
      mapped_key = mapped_key_config[key] || mapped_key
      puts "transport mapped key is #{mapped_key}"
      output_event(:mapped_key, state, wm_class_name)
      self.modifier_key = nil
    else # normal combine (e.g ctrl+c ctrl+v)
      output_combine(mapped_modifier_key, key, state, wm_class_name)
    end
  else
    output_combine(mapped_modifier_key, key, state, wm_class_name)
  end
end