Class: Uinput::Keyboard

Inherits:
Device
  • Object
show all
Defined in:
lib/uinput/keyboard.rb,
lib/uinput/keyboard/system_initializer.rb

Defined Under Namespace

Classes: SystemInitializer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keymap, &block) ⇒ Keyboard

Returns a new instance of Keyboard.



15
16
17
18
# File 'lib/uinput/keyboard.rb', line 15

def initialize(keymap, &block)
  @keymap = keymap
  super(&block)
end

Instance Attribute Details

#keymapObject

Returns the value of attribute keymap.



20
21
22
# File 'lib/uinput/keyboard.rb', line 20

def keymap
  @keymap
end

Class Method Details

.keymap(names) ⇒ Object



10
11
12
# File 'lib/uinput/keyboard.rb', line 10

def keymap(names)
  Xkbcommon::Context.new.keymap_from_names(names)
end

Instance Method Details

#press(*symbols) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/uinput/keyboard.rb', line 22

def press(*symbols)
  keys = symbols_to_keys(*symbols)
  keys.each do |key|
    send_event(:EV_KEY, key.scan_code, 1)
  end
  send_event(:EV_SYN, :SYN_REPORT)
  keys.map(&:code)
end

#release(*symbols) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/uinput/keyboard.rb', line 31

def release(*symbols)
  keys = symbols_to_keys(*symbols)
  keys.each do |key|
    send_event(:EV_KEY, key.scan_code, 0)
  end
  send_event(:EV_SYN, :SYN_REPORT)
  keys.map(&:code)
end

#string_to_keycodes(string) ⇒ Object



60
61
62
# File 'lib/uinput/keyboard.rb', line 60

def string_to_keycodes(string)
  string_to_symbols(string).map{ |symbols| symbols_to_keycodes(*symbols) }
end

#symbols_to_keycodes(*names) ⇒ Object



56
57
58
# File 'lib/uinput/keyboard.rb', line 56

def symbols_to_keycodes(*names)
  symbols_to_keys(*names).map(&:code)
end

#tap(*symbols) ⇒ Object



40
41
42
43
# File 'lib/uinput/keyboard.rb', line 40

def tap(*symbols)
  press(*symbols)
  release(*symbols)
end

#type(string) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/uinput/keyboard.rb', line 45

def type(string)
  # prevent dropped key events (b/c of evdev buffer overflow?) by sending
  # out the event chain in chunks rather than in one piece
  keycodes = []
  string_to_symbols(string).each_slice(8) do |symbols|
    keycodes += symbols.map{ |symbol| tap(symbol) }
    sleep 0.01 # time to breath
  end
  keycodes
end