Class: Uinput::Keyboard

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

Defined Under Namespace

Classes: Initializer

Constant Summary collapse

VERSION =
"0.3.1"

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
30
31
# File 'lib/uinput/keyboard.rb', line 22

def press(*symbols)
  symbols.flat_map do |symbol|
    symbol(symbol).keys.map do |key|
      send_event(:EV_KEY, key.scan_code, 1)
      key.code
    end.tap do
      send_event(:EV_SYN, :SYN_REPORT)
    end
  end
end

#release(*symbols) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/uinput/keyboard.rb', line 33

def release(*symbols)
  symbols.flat_map do |symbol|
    symbol(symbol).keys.map do |key|
      send_event(:EV_KEY, key.scan_code, 0)
      key.code
    end.tap do
      send_event(:EV_SYN, :SYN_REPORT)
    end
  end
end

#string_to_symbols(string) ⇒ Object



64
65
66
# File 'lib/uinput/keyboard.rb', line 64

def string_to_symbols(string)
  string.chars.map{ |char| char_to_symbol(char).name }
end

#symbols_to_keycodes(*names) ⇒ Object



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

def symbols_to_keycodes(*names)
  names.map{ |name| symbol(name).keys.map(&:code) }
end

#tap(*symbols) ⇒ Object



44
45
46
47
# File 'lib/uinput/keyboard.rb', line 44

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

#type(string) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/uinput/keyboard.rb', line 49

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