Module: WebkitRemote::Client::Input

Included in:
WebkitRemote::Client
Defined in:
lib/webkit_remote/client/input.rb

Overview

API for the Input domain.

Instance Method Summary collapse

Instance Method Details

#key_event(type, opts) ⇒ WebkitRemote::Client

Dispatches a keyboard event.

Parameters:

  • type (Symbol)

    the event type (:char, :down, :up, :raw_down)

  • opts (Hash)

    optional information

Options Hash (opts):

  • modifiers (Array<Symbol>)

    combination of :alt, :ctrl, :shift, and :command / :meta (empty by default)

  • time (Number)

    the event’s time, as a JavaScript timestamp

  • clicks (Number)

    number of times the mouse button was clicked (0 by default)

  • text (String)

    as generated by processing a virtual key code with a keyboard layout; not needed for :up and :raw_down events; (” by default)

  • unmodified_text (String)

    text that would have been generated by the keyboard if no modifiers were pressed (except for shift); useful for shortcut (accelerator) key handling; (” by default)

  • vkey (Number)

    the Windows virtual key code for the key; see developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Virtual_key_codes

  • key_id (Number)

    the unique key identifier (e.g., ‘U+0041’); (” by default)

  • auto_repeat (Boolean)

    true if the event was generated by an auto-repeat while the key was being held down

  • keypad (Boolean)

    true if the event was generated from the keypad

  • system_key (Boolean)

    true if the event was a system key event

Returns:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/webkit_remote/client/input.rb', line 82

def key_event(type, opts)
  options = {}
  options[:type] = case type
  when :char
    'char'
  when :down
    'keyDown'
  when :up
    'keyUp'
  when :raw_down
    'rawKeyDown'
  else
    raise RuntimeError, "Unsupported keyboard event type #{type}"
  end

  options[:timestamp] = opts[:time] if opts[:time]
  if opts[:modifiers]
    flags = 0
    opts[:modifiers].each do |modifier|
      flags |= case modifier
      when :alt
        1
      when :ctrl
        2
      when :command, :meta
        4
      when :shift
        8
      end
    end
    options[:modifiers] = flags
  end

  options[:keyIdentifier] = opts[:key_id] if opts[:key_id]
  options[:windowsVirtualKeyCode] = opts[:vkey] if opts[:vkey]
  options[:unmodifiedText] = opts[:unmodified_text] if opts[:unmodified_text]
  if opts[:text]
    options[:text] = opts[:text]
    options[:unmodifiedText] ||= opts[:text]
  end
  options[:autoRepeat] = true if opts[:auto_repeat]
  options[:isKeypad] = true if opts[:keypad]
  options[:isSystemKey] = true if opts[:system_key]

  @rpc.call 'Input.dispatchKeyEvent', options
  self
end

#mouse_event(type, x, y, opts = {}) ⇒ WebkitRemote::Client

Dispatches a mouse event.

Parameters:

  • type (Symbol)

    the event type (:move, :down, :up)

  • x (Integer)

    the X coordinate, relative to the main frame’s viewport

  • y (Integer)

    the Y coordinate, relative to the main frame’s viewport

  • opts (Hash) (defaults to: {})

    optional information

Options Hash (opts):

  • button (Symbol)

    :left, :right, :middle, or nil (none); nil by default

  • modifiers (Array<Symbol>)

    combination of :alt, :ctrl, :shift, and :command / :meta (empty by default)

  • time (Number)

    the event’s time, as a JavaScript timestamp

  • clicks (Number)

    number of times the mouse button was clicked (0 by default)

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/webkit_remote/client/input.rb', line 21

def mouse_event(type, x, y, opts = {})
  options = { x: x, y: y }
  options[:type] = case type
  when :move
    'mouseMoved'
  when :down
    'mousePressed'
  when :up
    'mouseReleased'
  else
    raise RuntimeError, "Unsupported mouse event type #{type}"
  end

  options[:timestamp] = opts[:time] if opts[:time]
  options[:clickCount] = opts[:clicks] if opts[:clicks]
  if opts[:modifiers]
    flags = 0
    opts[:modifiers].each do |modifier|
      flags |= case modifier
      when :alt
        1
      when :ctrl
        2
      when :command, :meta
        4
      when :shift
        8
      end
    end
    options[:modifiers] = flags
  end

  @rpc.call 'Input.dispatchMouseEvent', options
  self
end