Class: Ferrum::Keyboard

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

Overview

Simulates keyboard input for a page via the CDP Input.dispatchKeyEvent command. Provides low-level down/up for individual keys as well as type for typing a sequence of characters/keys, translating key names and modifier chords into the CDP key event parameters Chrome expects.

Constant Summary collapse

KEYS =
JSON.parse(File.read(File.expand_path("keyboard.json", __dir__)))
MODIFIERS =
{ "alt" => 1, "ctrl" => 2, "control" => 2,
"meta" => 4, "command" => 4, "shift" => 8 }.freeze
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"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ Keyboard

Returns a new instance of Keyboard.



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

def initialize(page)
  @page = page
end

Instance Method Details

#down(key) ⇒ self

Dispatches a keydown event.

Parameters:

  • key (String, Symbol)

    Name of the key, such as "a", :enter, or :backspace.

Returns:

  • (self)


47
48
49
50
51
52
# File 'lib/ferrum/keyboard.rb', line 47

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

#modifiers(keys) ⇒ Integer

Returns bitfield for a given keys.

Parameters:

  • keys (Array<:alt, :ctrl, :command, :shift>)

Returns:

  • (Integer)


96
97
98
# File 'lib/ferrum/keyboard.rb', line 96

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

#type(*keys) ⇒ self

Sends a keydown, keypress/input, and keyup event for each character in the text.

Parameters:

  • keys (Array<String, Symbol, (Symbol, String)>)

    The text to type into a focused element, [:Shift, "s"], "tring".

Returns:

  • (self)


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ferrum/keyboard.rb', line 77

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", slowmoable: true, type: "keyUp", **key)
  end

  self
end

#up(key) ⇒ self

Dispatches a keyup event.

Parameters:

  • key (String, Symbol)

    Name of the key, such as "a", :enter, or :backspace.

Returns:

  • (self)


62
63
64
65
66
# File 'lib/ferrum/keyboard.rb', line 62

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