Class: Puppeteer::Bidi::Keyboard
- Inherits:
-
Object
- Object
- Puppeteer::Bidi::Keyboard
- Defined in:
- lib/puppeteer/bidi/keyboard.rb,
sig/puppeteer/bidi/keyboard.rbs
Overview
Keyboard class for keyboard input operations Based on Puppeteer's BidiKeyboard implementation
Instance Method Summary collapse
-
#down(key, text: nil, commands: nil) ⇒ void
Press key down.
-
#get_bidi_key_value(key) ⇒ String
Convert key name to BiDi protocol value.
-
#initialize(page, browsing_context) ⇒ Keyboard
constructor
A new instance of Keyboard.
-
#perform_actions(action_list) ⇒ void
Perform input actions via BiDi.
-
#press(key, delay: nil, text: nil, commands: nil) ⇒ void
Press and release a key.
-
#send_character(char) ⇒ void
Send character directly (bypasses keyboard events, uses execCommand).
-
#type(text, delay: 0) ⇒ void
Type text (types each character with keydown/keyup events).
-
#up(key) ⇒ void
Release key.
Constructor Details
#initialize(page, browsing_context) ⇒ Keyboard
Returns a new instance of Keyboard.
12 13 14 15 16 |
# File 'lib/puppeteer/bidi/keyboard.rb', line 12 def initialize(page, browsing_context) @page = page @browsing_context = browsing_context @pressed_keys = Set.new end |
Instance Method Details
#down(key, text: nil, commands: nil) ⇒ void
This method returns an undefined value.
Press key down
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/puppeteer/bidi/keyboard.rb', line 23 def down(key, text: nil, commands: nil) # Note: text and commands parameters exist for CDP compatibility but are not used in BiDi actions = [{ type: 'keyDown', value: get_bidi_key_value(key) }] perform_actions(actions) @pressed_keys.add(key) end |
#get_bidi_key_value(key) ⇒ String
Convert key name to BiDi protocol value
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/puppeteer/bidi/keyboard.rb', line 120 def get_bidi_key_value(key) # Normalize line breaks normalized_key = case key when "\r", "\n" then 'Enter' else key end # If it's a single character (measured by code points), return as-is if normalized_key.length == 1 return normalized_key end # Map key names to WebDriver BiDi Unicode values # Reference: https://w3c.github.io/webdriver/#keyboard-actions case normalized_key # Modifier keys when 'Shift', 'ShiftLeft' then "\uE008" when 'ShiftRight' then "\uE050" when 'Ctrl', 'Control', 'CtrlLeft', 'ControlLeft' then "\uE009" when 'CtrlRight', 'ControlRight' then "\uE051" when 'Alt', 'AltLeft' then "\uE00A" when 'AltRight' then "\uE052" when 'Meta', 'MetaLeft' then "\uE03D" when 'MetaRight' then "\uE053" when 'CtrlOrMeta', 'ControlOrMeta' then ( if RUBY_PLATFORM.include?('darwin') "\uE03D" # Meta else "\uE009" # Control end ) # Whitespace keys when 'Enter', 'NumpadEnter' then "\uE007" when 'Tab' then "\uE004" when 'Space' then "\uE00D" # Editing keys when 'Backspace' then "\uE003" when 'Delete' then "\uE017" when 'Escape' then "\uE00C" # Navigation keys when 'ArrowUp' then "\uE013" when 'ArrowDown' then "\uE015" when 'ArrowLeft' then "\uE012" when 'ArrowRight' then "\uE014" when 'Home' then "\uE011" when 'End' then "\uE010" when 'PageUp' then "\uE00E" when 'PageDown' then "\uE00F" when 'Insert' then "\uE016" # Function keys when 'F1' then "\uE031" when 'F2' then "\uE032" when 'F3' then "\uE033" when 'F4' then "\uE034" when 'F5' then "\uE035" when 'F6' then "\uE036" when 'F7' then "\uE037" when 'F8' then "\uE038" when 'F9' then "\uE039" when 'F10' then "\uE03A" when 'F11' then "\uE03B" when 'F12' then "\uE03C" # Numpad when 'Numpad0' then "\uE01A" when 'Numpad1' then "\uE01B" when 'Numpad2' then "\uE01C" when 'Numpad3' then "\uE01D" when 'Numpad4' then "\uE01E" when 'Numpad5' then "\uE01F" when 'Numpad6' then "\uE020" when 'Numpad7' then "\uE021" when 'Numpad8' then "\uE022" when 'Numpad9' then "\uE023" when 'NumpadMultiply' then "\uE024" when 'NumpadAdd' then "\uE025" when 'NumpadSubtract' then "\uE027" when 'NumpadDecimal' then "\uE028" when 'NumpadDivide' then "\uE029" # Key codes - convert to actual characters when 'Digit0' then '0' when 'Digit1' then '1' when 'Digit2' then '2' when 'Digit3' then '3' when 'Digit4' then '4' when 'Digit5' then '5' when 'Digit6' then '6' when 'Digit7' then '7' when 'Digit8' then '8' when 'Digit9' then '9' when 'KeyA' then 'a' when 'KeyB' then 'b' when 'KeyC' then 'c' when 'KeyD' then 'd' when 'KeyE' then 'e' when 'KeyF' then 'f' when 'KeyG' then 'g' when 'KeyH' then 'h' when 'KeyI' then 'i' when 'KeyJ' then 'j' when 'KeyK' then 'k' when 'KeyL' then 'l' when 'KeyM' then 'm' when 'KeyN' then 'n' when 'KeyO' then 'o' when 'KeyP' then 'p' when 'KeyQ' then 'q' when 'KeyR' then 'r' when 'KeyS' then 's' when 'KeyT' then 't' when 'KeyU' then 'u' when 'KeyV' then 'v' when 'KeyW' then 'w' when 'KeyX' then 'x' when 'KeyY' then 'y' when 'KeyZ' then 'z' # Punctuation when 'Semicolon' then ';' when 'Equal' then '=' when 'Comma' then ',' when 'Minus' then '-' when 'Period' then '.' when 'Slash' then '/' when 'Backquote' then '`' when 'BracketLeft' then '[' when 'Backslash' then '\\' when 'BracketRight' then ']' when 'Quote' then "'" else raise "Unknown key: #{normalized_key.inspect}" end end |
#perform_actions(action_list) ⇒ void
This method returns an undefined value.
Perform input actions via BiDi
265 266 267 268 269 270 271 272 273 |
# File 'lib/puppeteer/bidi/keyboard.rb', line 265 def perform_actions(action_list) @browsing_context.perform_actions([ { type: 'key', id: 'default keyboard', actions: action_list } ]).wait end |
#press(key, delay: nil, text: nil, commands: nil) ⇒ void
This method returns an undefined value.
Press and release a key
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/puppeteer/bidi/keyboard.rb', line 53 def press(key, delay: nil, text: nil, commands: nil) # Note: text and commands parameters exist for CDP compatibility but are not used in BiDi actions = [{ type: 'keyDown', value: get_bidi_key_value(key) }] if delay actions << { type: 'pause', duration: delay.to_i } end actions << { type: 'keyUp', value: get_bidi_key_value(key) } perform_actions(actions) end |
#send_character(char) ⇒ void
This method returns an undefined value.
Send character directly (bypasses keyboard events, uses execCommand)
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/puppeteer/bidi/keyboard.rb', line 97 def send_character(char) # Validate: cannot send more than 1 character # Measures the number of code points rather than UTF-16 code units if char.chars.length > 1 raise ArgumentError, 'Cannot send more than 1 character.' end # Get the focused frame (may be an iframe) focused_frame = @page.focused_frame # Execute insertText in the focused frame's isolated realm focused_frame.isolated_realm.call_function( 'function(char) { document.execCommand("insertText", false, char); }', false, arguments: [{ type: 'string', value: char }] ) end |
#type(text, delay: 0) ⇒ void
This method returns an undefined value.
Type text (types each character with keydown/keyup events)
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/puppeteer/bidi/keyboard.rb', line 73 def type(text, delay: 0) actions = [] # Split text into individual code points (handles multi-byte Unicode correctly) text.each_char do |char| key_value = get_bidi_key_value(char) actions << { type: 'keyDown', value: key_value } actions << { type: 'keyUp', value: key_value } if delay > 0 actions << { type: 'pause', duration: delay.to_i } end end perform_actions(actions) end |
#up(key) ⇒ void
This method returns an undefined value.
Release key
37 38 39 40 41 42 43 44 45 |
# File 'lib/puppeteer/bidi/keyboard.rb', line 37 def up(key) actions = [{ type: 'keyUp', value: get_bidi_key_value(key) }] perform_actions(actions) @pressed_keys.delete(key) end |