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", __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.



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

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)


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

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)


90
91
92
# File 'lib/ferrum/keyboard.rb', line 90

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)


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ferrum/keyboard.rb', line 71

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)


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

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