Class: Ferrum::Keyboard

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

Constant Summary collapse

KEYS =
JSON.parse(File.read(File.expand_path("../keyboard.json", __FILE__)))
MODIFIERS =
{ "alt" => 1, "ctrl" => 2, "control" => 2,
"meta" => 4, "command" => 4, "shift" => 8 }
KEYS_MAPPING =
{
  cancel: "Cancel", help: "Help", backspace: "Backspace", tab: "Tab",
  clear: "Clear", return: "Enter", enter: "Enter", shift: "Shift",
  ctrl: "Control", control: "Control", alt: "Alt", pause: "Pause",
  escape: "Escape", space: "Space",  pageup: "PageUp", page_up: "PageUp",
  pagedown: "PageDown", page_down: "PageDown", end: "End", home: "Home",
  left: "ArrowLeft", up: "ArrowUp", right: "ArrowRight",
  down: "ArrowDown", insert: "Insert", delete: "Delete",
  semicolon: "Semicolon", equals: "Equal", numpad0: "Numpad0",
  numpad1: "Numpad1", numpad2: "Numpad2", numpad3: "Numpad3",
  numpad4: "Numpad4", numpad5: "Numpad5", numpad6: "Numpad6",
  numpad7: "Numpad7", numpad8: "Numpad8", numpad9: "Numpad9",
  multiply: "NumpadMultiply", add: "NumpadAdd",
  separator: "NumpadDecimal", subtract: "NumpadSubtract",
  decimal: "NumpadDecimal", divide: "NumpadDivide", f1: "F1", f2: "F2",
  f3: "F3", f4: "F4", f5: "F5", f6: "F6", f7: "F7", f8: "F8", f9: "F9",
  f10: "F10", f11: "F11", f12: "F12", meta: "Meta", command: "Meta",
}

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ Keyboard

Returns a new instance of Keyboard.



29
30
31
# File 'lib/ferrum/keyboard.rb', line 29

def initialize(page)
  @page = page
end

Instance Method Details

#down(key) ⇒ Object



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

def down(key)
  key = normalize_keys(Array(key))
  type = key[:text] ? "keyDown" : "rawKeyDown"
  @page.command("Input.dispatchKeyEvent", type: type, **key)
  self
end

#modifiers(keys) ⇒ Object



58
59
60
# File 'lib/ferrum/keyboard.rb', line 58

def modifiers(keys)
  keys.map { |k| MODIFIERS[k.to_s] }.compact.reduce(0, :|)
end

#type(*keys) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ferrum/keyboard.rb', line 46

def type(*keys)
  keys = normalize_keys(Array(keys))

  keys.each do |key|
    type = key[:text] ? "keyDown" : "rawKeyDown"
    @page.command("Input.dispatchKeyEvent", type: type, **key)
    @page.command("Input.dispatchKeyEvent", type: "keyUp", **key)
  end

  self
end

#up(key) ⇒ Object



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

def up(key)
  key = normalize_keys(Array(key))
  @page.command("Input.dispatchKeyEvent", type: "keyUp", **key)
  self
end