@selenium/atoms 4.51.0-nightly202609302012 → 4.52.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +40 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +40 -0
- package/dist/index.js.map +1 -1
- package/dist/typescript/active-element.d.ts +2 -0
- package/dist/typescript/active-element.d.ts.map +1 -0
- package/dist/typescript/active-element.js +19 -0
- package/dist/typescript/active-element.js.map +1 -0
- package/dist/typescript/clear-local-storage.d.ts +3 -0
- package/dist/typescript/clear-local-storage.d.ts.map +1 -0
- package/dist/typescript/clear-local-storage.js +25 -0
- package/dist/typescript/clear-local-storage.js.map +1 -0
- package/dist/typescript/clear-session-storage.d.ts +3 -0
- package/dist/typescript/clear-session-storage.d.ts.map +1 -0
- package/dist/typescript/clear-session-storage.js +25 -0
- package/dist/typescript/clear-session-storage.js.map +1 -0
- package/dist/typescript/clear.d.ts +3 -0
- package/dist/typescript/clear.d.ts.map +1 -0
- package/dist/typescript/clear.js +67 -0
- package/dist/typescript/clear.js.map +1 -0
- package/dist/typescript/click.d.ts +4 -0
- package/dist/typescript/click.d.ts.map +1 -0
- package/dist/typescript/click.js +52 -0
- package/dist/typescript/click.js.map +1 -0
- package/dist/typescript/core/attribute.d.ts +3 -0
- package/dist/typescript/core/attribute.d.ts.map +1 -0
- package/dist/typescript/core/attribute.js +60 -0
- package/dist/typescript/core/attribute.js.map +1 -0
- package/dist/typescript/core/color.d.ts +2 -0
- package/dist/typescript/core/color.d.ts.map +1 -0
- package/dist/typescript/core/color.js +248 -0
- package/dist/typescript/core/color.js.map +1 -0
- package/dist/typescript/core/editable.d.ts +5 -0
- package/dist/typescript/core/editable.d.ts.map +1 -0
- package/dist/typescript/core/editable.js +55 -0
- package/dist/typescript/core/editable.js.map +1 -0
- package/dist/typescript/core/element.d.ts +6 -0
- package/dist/typescript/core/element.d.ts.map +1 -0
- package/dist/typescript/core/element.js +97 -0
- package/dist/typescript/core/element.js.map +1 -0
- package/dist/typescript/core/enabled.d.ts +2 -0
- package/dist/typescript/core/enabled.d.ts.map +1 -0
- package/dist/typescript/core/enabled.js +69 -0
- package/dist/typescript/core/enabled.js.map +1 -0
- package/dist/typescript/core/error.d.ts +13 -0
- package/dist/typescript/core/error.d.ts.map +1 -0
- package/dist/typescript/core/error.js +40 -0
- package/dist/typescript/core/error.js.map +1 -0
- package/dist/typescript/core/events.d.ts +91 -0
- package/dist/typescript/core/events.d.ts.map +1 -0
- package/dist/typescript/core/events.js +131 -0
- package/dist/typescript/core/events.js.map +1 -0
- package/dist/typescript/core/focus.d.ts +4 -0
- package/dist/typescript/core/focus.d.ts.map +1 -0
- package/dist/typescript/core/focus.js +88 -0
- package/dist/typescript/core/focus.js.map +1 -0
- package/dist/typescript/core/focusable.d.ts +2 -0
- package/dist/typescript/core/focusable.d.ts.map +1 -0
- package/dist/typescript/core/focusable.js +36 -0
- package/dist/typescript/core/focusable.js.map +1 -0
- package/dist/typescript/core/form.d.ts +2 -0
- package/dist/typescript/core/form.d.ts.map +1 -0
- package/dist/typescript/core/form.js +76 -0
- package/dist/typescript/core/form.js.map +1 -0
- package/dist/typescript/core/frame.d.ts +8 -0
- package/dist/typescript/core/frame.d.ts.map +1 -0
- package/dist/typescript/core/frame.js +95 -0
- package/dist/typescript/core/frame.js.map +1 -0
- package/dist/typescript/core/geometry.d.ts +28 -0
- package/dist/typescript/core/geometry.d.ts.map +1 -0
- package/dist/typescript/core/geometry.js +134 -0
- package/dist/typescript/core/geometry.js.map +1 -0
- package/dist/typescript/core/inject.d.ts +21 -0
- package/dist/typescript/core/inject.d.ts.map +1 -0
- package/dist/typescript/core/inject.js +282 -0
- package/dist/typescript/core/inject.js.map +1 -0
- package/dist/typescript/core/interactable.d.ts +2 -0
- package/dist/typescript/core/interactable.d.ts.map +1 -0
- package/dist/typescript/core/interactable.js +27 -0
- package/dist/typescript/core/interactable.js.map +1 -0
- package/dist/typescript/core/keyboard.d.ts +15 -0
- package/dist/typescript/core/keyboard.d.ts.map +1 -0
- package/dist/typescript/core/keyboard.js +356 -0
- package/dist/typescript/core/keyboard.js.map +1 -0
- package/dist/typescript/core/keys.d.ts +65 -0
- package/dist/typescript/core/keys.d.ts.map +1 -0
- package/dist/typescript/core/keys.js +125 -0
- package/dist/typescript/core/keys.js.map +1 -0
- package/dist/typescript/core/locators.d.ts +6 -0
- package/dist/typescript/core/locators.d.ts.map +1 -0
- package/dist/typescript/core/locators.js +610 -0
- package/dist/typescript/core/locators.js.map +1 -0
- package/dist/typescript/core/mouse.d.ts +17 -0
- package/dist/typescript/core/mouse.d.ts.map +1 -0
- package/dist/typescript/core/mouse.js +186 -0
- package/dist/typescript/core/mouse.js.map +1 -0
- package/dist/typescript/core/overflow.d.ts +5 -0
- package/dist/typescript/core/overflow.d.ts.map +1 -0
- package/dist/typescript/core/overflow.js +133 -0
- package/dist/typescript/core/overflow.js.map +1 -0
- package/dist/typescript/core/scroll.d.ts +3 -0
- package/dist/typescript/core/scroll.d.ts.map +1 -0
- package/dist/typescript/core/scroll.js +77 -0
- package/dist/typescript/core/scroll.js.map +1 -0
- package/dist/typescript/core/shown.d.ts +3 -0
- package/dist/typescript/core/shown.d.ts.map +1 -0
- package/dist/typescript/core/shown.js +190 -0
- package/dist/typescript/core/shown.js.map +1 -0
- package/dist/typescript/core/storage.d.ts +14 -0
- package/dist/typescript/core/storage.d.ts.map +1 -0
- package/dist/typescript/core/storage.js +98 -0
- package/dist/typescript/core/storage.js.map +1 -0
- package/dist/typescript/core/style.d.ts +2 -0
- package/dist/typescript/core/style.d.ts.map +1 -0
- package/dist/typescript/core/style.js +32 -0
- package/dist/typescript/core/style.js.map +1 -0
- package/dist/typescript/core/useragent.d.ts +3 -0
- package/dist/typescript/core/useragent.d.ts.map +1 -0
- package/dist/typescript/core/useragent.js +31 -0
- package/dist/typescript/core/useragent.js.map +1 -0
- package/dist/typescript/default-content.d.ts +2 -0
- package/dist/typescript/default-content.d.ts.map +1 -0
- package/dist/typescript/default-content.js +20 -0
- package/dist/typescript/default-content.js.map +1 -0
- package/dist/typescript/execute-async-script.d.ts +2 -0
- package/dist/typescript/execute-async-script.d.ts.map +1 -0
- package/dist/typescript/execute-async-script.js +20 -0
- package/dist/typescript/execute-async-script.js.map +1 -0
- package/dist/typescript/execute-script.d.ts +2 -0
- package/dist/typescript/execute-script.d.ts.map +1 -0
- package/dist/typescript/execute-script.js +20 -0
- package/dist/typescript/execute-script.js.map +1 -0
- package/dist/typescript/find-element-fragment.d.ts +8 -0
- package/dist/typescript/find-element-fragment.d.ts.map +1 -0
- package/dist/typescript/find-element-fragment.js +39 -0
- package/dist/typescript/find-element-fragment.js.map +1 -0
- package/dist/typescript/find-element.d.ts +2 -0
- package/dist/typescript/find-element.d.ts.map +1 -0
- package/dist/typescript/find-element.js +18 -0
- package/dist/typescript/find-element.js.map +1 -0
- package/dist/typescript/find-elements.d.ts +1 -2
- package/dist/typescript/find-elements.d.ts.map +1 -1
- package/dist/typescript/find-elements.js +1 -289
- package/dist/typescript/find-elements.js.map +1 -1
- package/dist/typescript/frame-by-id-or-name.d.ts +2 -0
- package/dist/typescript/frame-by-id-or-name.d.ts.map +1 -0
- package/dist/typescript/frame-by-id-or-name.js +22 -0
- package/dist/typescript/frame-by-id-or-name.js.map +1 -0
- package/dist/typescript/frame-by-index.d.ts +2 -0
- package/dist/typescript/frame-by-index.d.ts.map +1 -0
- package/dist/typescript/frame-by-index.js +19 -0
- package/dist/typescript/frame-by-index.js.map +1 -0
- package/dist/typescript/get-attribute-value.d.ts +2 -0
- package/dist/typescript/get-attribute-value.d.ts.map +1 -0
- package/dist/typescript/get-attribute-value.js +22 -0
- package/dist/typescript/get-attribute-value.js.map +1 -0
- package/dist/typescript/get-effective-style.d.ts +3 -0
- package/dist/typescript/get-effective-style.d.ts.map +1 -0
- package/dist/typescript/get-effective-style.js +84 -0
- package/dist/typescript/get-effective-style.js.map +1 -0
- package/dist/typescript/get-element-from-cache.d.ts +2 -0
- package/dist/typescript/get-element-from-cache.d.ts.map +1 -0
- package/dist/typescript/get-element-from-cache.js +20 -0
- package/dist/typescript/get-element-from-cache.js.map +1 -0
- package/dist/typescript/get-frame-window.d.ts +2 -0
- package/dist/typescript/get-frame-window.d.ts.map +1 -0
- package/dist/typescript/get-frame-window.js +22 -0
- package/dist/typescript/get-frame-window.js.map +1 -0
- package/dist/typescript/get-local-storage-item.d.ts +3 -0
- package/dist/typescript/get-local-storage-item.d.ts.map +1 -0
- package/dist/typescript/get-local-storage-item.js +25 -0
- package/dist/typescript/get-local-storage-item.js.map +1 -0
- package/dist/typescript/get-local-storage-key.d.ts +3 -0
- package/dist/typescript/get-local-storage-key.d.ts.map +1 -0
- package/dist/typescript/get-local-storage-key.js +26 -0
- package/dist/typescript/get-local-storage-key.js.map +1 -0
- package/dist/typescript/get-local-storage-keys.d.ts +3 -0
- package/dist/typescript/get-local-storage-keys.d.ts.map +1 -0
- package/dist/typescript/get-local-storage-keys.js +25 -0
- package/dist/typescript/get-local-storage-keys.js.map +1 -0
- package/dist/typescript/get-local-storage-size.d.ts +3 -0
- package/dist/typescript/get-local-storage-size.d.ts.map +1 -0
- package/dist/typescript/get-local-storage-size.js +25 -0
- package/dist/typescript/get-local-storage-size.js.map +1 -0
- package/dist/typescript/get-location.d.ts +9 -0
- package/dist/typescript/get-location.d.ts.map +1 -0
- package/dist/typescript/get-location.js +49 -0
- package/dist/typescript/get-location.js.map +1 -0
- package/dist/typescript/get-session-storage-item.d.ts +3 -0
- package/dist/typescript/get-session-storage-item.d.ts.map +1 -0
- package/dist/typescript/get-session-storage-item.js +25 -0
- package/dist/typescript/get-session-storage-item.js.map +1 -0
- package/dist/typescript/get-session-storage-key.d.ts +3 -0
- package/dist/typescript/get-session-storage-key.d.ts.map +1 -0
- package/dist/typescript/get-session-storage-key.js +26 -0
- package/dist/typescript/get-session-storage-key.js.map +1 -0
- package/dist/typescript/get-session-storage-keys.d.ts +3 -0
- package/dist/typescript/get-session-storage-keys.d.ts.map +1 -0
- package/dist/typescript/get-session-storage-keys.js +25 -0
- package/dist/typescript/get-session-storage-keys.js.map +1 -0
- package/dist/typescript/get-session-storage-size.d.ts +3 -0
- package/dist/typescript/get-session-storage-size.d.ts.map +1 -0
- package/dist/typescript/get-session-storage-size.js +25 -0
- package/dist/typescript/get-session-storage-size.js.map +1 -0
- package/dist/typescript/get-size.d.ts +7 -0
- package/dist/typescript/get-size.d.ts.map +1 -0
- package/dist/typescript/get-size.js +36 -0
- package/dist/typescript/get-size.js.map +1 -0
- package/dist/typescript/get-text.d.ts +3 -0
- package/dist/typescript/get-text.d.ts.map +1 -0
- package/dist/typescript/get-text.js +294 -0
- package/dist/typescript/get-text.js.map +1 -0
- package/dist/typescript/get-top-left-coordinates.d.ts +4 -0
- package/dist/typescript/get-top-left-coordinates.d.ts.map +1 -0
- package/dist/typescript/get-top-left-coordinates.js +36 -0
- package/dist/typescript/get-top-left-coordinates.js.map +1 -0
- package/dist/typescript/is-displayed.d.ts +1 -2
- package/dist/typescript/is-displayed.d.ts.map +1 -1
- package/dist/typescript/is-displayed.js +3 -396
- package/dist/typescript/is-displayed.js.map +1 -1
- package/dist/typescript/is-editable.d.ts +2 -0
- package/dist/typescript/is-editable.d.ts.map +1 -0
- package/dist/typescript/is-editable.js +18 -0
- package/dist/typescript/is-editable.js.map +1 -0
- package/dist/typescript/is-enabled.d.ts +2 -0
- package/dist/typescript/is-enabled.d.ts.map +1 -0
- package/dist/typescript/is-enabled.js +18 -0
- package/dist/typescript/is-enabled.js.map +1 -0
- package/dist/typescript/is-focusable.d.ts +2 -0
- package/dist/typescript/is-focusable.d.ts.map +1 -0
- package/dist/typescript/is-focusable.js +20 -0
- package/dist/typescript/is-focusable.js.map +1 -0
- package/dist/typescript/is-interactable.d.ts +2 -0
- package/dist/typescript/is-interactable.d.ts.map +1 -0
- package/dist/typescript/is-interactable.js +19 -0
- package/dist/typescript/is-interactable.js.map +1 -0
- package/dist/typescript/is-selected.d.ts +3 -0
- package/dist/typescript/is-selected.d.ts.map +1 -0
- package/dist/typescript/is-selected.js +29 -0
- package/dist/typescript/is-selected.js.map +1 -0
- package/dist/typescript/remove-local-storage-item.d.ts +3 -0
- package/dist/typescript/remove-local-storage-item.d.ts.map +1 -0
- package/dist/typescript/remove-local-storage-item.js +25 -0
- package/dist/typescript/remove-local-storage-item.js.map +1 -0
- package/dist/typescript/remove-session-storage-item.d.ts +3 -0
- package/dist/typescript/remove-session-storage-item.d.ts.map +1 -0
- package/dist/typescript/remove-session-storage-item.js +25 -0
- package/dist/typescript/remove-session-storage-item.js.map +1 -0
- package/dist/typescript/set-local-storage-item.d.ts +3 -0
- package/dist/typescript/set-local-storage-item.d.ts.map +1 -0
- package/dist/typescript/set-local-storage-item.js +26 -0
- package/dist/typescript/set-local-storage-item.js.map +1 -0
- package/dist/typescript/set-session-storage-item.d.ts +3 -0
- package/dist/typescript/set-session-storage-item.d.ts.map +1 -0
- package/dist/typescript/set-session-storage-item.js +26 -0
- package/dist/typescript/set-session-storage-item.js.map +1 -0
- package/dist/typescript/submit.d.ts +3 -0
- package/dist/typescript/submit.d.ts.map +1 -0
- package/dist/typescript/submit.js +50 -0
- package/dist/typescript/submit.js.map +1 -0
- package/dist/typescript/type.d.ts +3 -0
- package/dist/typescript/type.d.ts.map +1 -0
- package/dist/typescript/type.js +201 -0
- package/dist/typescript/type.js.map +1 -0
- package/index.ts +40 -0
- package/package.json +296 -5
- package/typescript/active-element.ts +19 -0
- package/typescript/clear-local-storage.ts +27 -0
- package/typescript/clear-session-storage.ts +27 -0
- package/typescript/clear.ts +73 -0
- package/typescript/click.ts +55 -0
- package/typescript/core/attribute.ts +68 -0
- package/typescript/core/color.ts +257 -0
- package/typescript/core/editable.ts +63 -0
- package/typescript/core/element.ts +110 -0
- package/typescript/core/enabled.ts +76 -0
- package/typescript/core/error.ts +41 -0
- package/typescript/core/events.ts +268 -0
- package/typescript/core/focus.ts +92 -0
- package/typescript/core/focusable.ts +38 -0
- package/typescript/core/form.ts +81 -0
- package/typescript/core/frame.ts +105 -0
- package/typescript/core/geometry.ts +183 -0
- package/typescript/core/inject.ts +336 -0
- package/typescript/core/interactable.ts +30 -0
- package/typescript/core/keyboard.ts +369 -0
- package/typescript/core/keys.ts +204 -0
- package/typescript/core/locators.ts +704 -0
- package/typescript/core/mouse.ts +211 -0
- package/typescript/core/overflow.ts +172 -0
- package/typescript/core/scroll.ts +92 -0
- package/typescript/core/shown.ts +233 -0
- package/typescript/core/storage.ts +111 -0
- package/typescript/core/style.ts +32 -0
- package/typescript/core/useragent.ts +33 -0
- package/typescript/default-content.ts +20 -0
- package/typescript/execute-async-script.ts +20 -0
- package/typescript/execute-script.ts +20 -0
- package/typescript/find-element-fragment.ts +45 -0
- package/typescript/find-element.ts +18 -0
- package/typescript/find-elements.ts +1 -326
- package/typescript/frame-by-id-or-name.ts +22 -0
- package/typescript/frame-by-index.ts +19 -0
- package/typescript/get-attribute-value.ts +22 -0
- package/typescript/get-effective-style.ts +93 -0
- package/typescript/get-element-from-cache.ts +20 -0
- package/typescript/get-frame-window.ts +22 -0
- package/typescript/get-local-storage-item.ts +27 -0
- package/typescript/get-local-storage-key.ts +28 -0
- package/typescript/get-local-storage-keys.ts +27 -0
- package/typescript/get-local-storage-size.ts +27 -0
- package/typescript/get-location.ts +63 -0
- package/typescript/get-session-storage-item.ts +27 -0
- package/typescript/get-session-storage-key.ts +28 -0
- package/typescript/get-session-storage-keys.ts +27 -0
- package/typescript/get-session-storage-size.ts +27 -0
- package/typescript/get-size.ts +43 -0
- package/typescript/get-text.ts +353 -0
- package/typescript/get-top-left-coordinates.ts +38 -0
- package/typescript/is-displayed.ts +3 -498
- package/typescript/is-editable.ts +18 -0
- package/typescript/is-enabled.ts +18 -0
- package/typescript/is-focusable.ts +20 -0
- package/typescript/is-interactable.ts +19 -0
- package/typescript/is-selected.ts +31 -0
- package/typescript/remove-local-storage-item.ts +27 -0
- package/typescript/remove-session-storage-item.ts +27 -0
- package/typescript/set-local-storage-item.ts +28 -0
- package/typescript/set-session-storage-item.ts +28 -0
- package/typescript/submit.ts +53 -0
- package/typescript/type.ts +210 -0
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
// Licensed to the Software Freedom Conservancy (SFC) under one
|
|
2
|
+
// or more contributor license agreements. See the NOTICE file
|
|
3
|
+
// distributed with this work for additional information
|
|
4
|
+
// regarding copyright ownership. The SFC licenses this file
|
|
5
|
+
// to you under the Apache License, Version 2.0 (the
|
|
6
|
+
// "License"); you may not use this file except in compliance
|
|
7
|
+
// with the License. You may obtain a copy of the License at
|
|
8
|
+
//
|
|
9
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
//
|
|
11
|
+
// Unless required by applicable law or agreed to in writing,
|
|
12
|
+
// software distributed under the License is distributed on an
|
|
13
|
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
+
// KIND, either express or implied. See the License for the
|
|
15
|
+
// specific language governing permissions and limitations
|
|
16
|
+
// under the License.
|
|
17
|
+
|
|
18
|
+
import { isContentEditable, isEditable, isFileInput } from './editable.js'
|
|
19
|
+
import { isElement } from './element.js'
|
|
20
|
+
import { UNKNOWN_ERROR, botError } from './error.js'
|
|
21
|
+
import { type EventFactory, EventType, type KeyboardArgs, fire } from './events.js'
|
|
22
|
+
import { focusOnElement, getActiveElementFrom, holdsFocus } from './focus.js'
|
|
23
|
+
import { implicitlySubmit } from './form.js'
|
|
24
|
+
import { type Key, Keys, isModifier, keyCodeFor } from './keys.js'
|
|
25
|
+
|
|
26
|
+
// A virtual keyboard that produces what a real key press produces: keydown,
|
|
27
|
+
// keypress for character keys and Enter, beforeinput / textInput / input
|
|
28
|
+
// around the edit, and keyup -- each with key, code and the engine's legacy
|
|
29
|
+
// codes -- and that performs the edit itself, since an untrusted key event has
|
|
30
|
+
// no default action.
|
|
31
|
+
//
|
|
32
|
+
// Replaces bot.Keyboard. Differences, all toward what a real keyboard does:
|
|
33
|
+
// - Events go to whichever element has focus when the key is pressed, so a
|
|
34
|
+
// page that moves focus from a handler (one-time-code fields, auto-advance)
|
|
35
|
+
// keeps receiving the typing where a user's would go. bot.Keyboard always
|
|
36
|
+
// typed into the element it started with.
|
|
37
|
+
// - beforeinput is fired, with inputType and data, and cancelling it cancels
|
|
38
|
+
// the edit; so does cancelling textInput.
|
|
39
|
+
// - contenteditable is edited. bot.Keyboard appended to an expando `value`
|
|
40
|
+
// there and never changed the DOM.
|
|
41
|
+
// - maxlength is respected, and a character outside the BMP is one key.
|
|
42
|
+
// - No keypress for Escape, arrows or Meta: Firefox stopped firing those.
|
|
43
|
+
// - Caret movement uses the element's own selection direction instead of a
|
|
44
|
+
// tracked position, and there is no serialisable state: none of the atoms
|
|
45
|
+
// that use the keyboard carry one across calls.
|
|
46
|
+
export class Keyboard {
|
|
47
|
+
private element: Element | null = null
|
|
48
|
+
private readonly pressed = new Set<Key>()
|
|
49
|
+
|
|
50
|
+
isPressed(key: Key): boolean {
|
|
51
|
+
return this.pressed.has(key)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Focuses the element, as clicking into it would, and when that moved focus
|
|
55
|
+
// puts the caret at the end of its text. A shadow host whose tree already
|
|
56
|
+
// holds focus is left alone: focusing the host itself would blur the field
|
|
57
|
+
// inside it that the keys are meant for.
|
|
58
|
+
moveCursor(element: Element): void {
|
|
59
|
+
this.element = element
|
|
60
|
+
if (!holdsFocus(element) && focusOnElement(element) && isEditable(element)) {
|
|
61
|
+
caretToEnd(element)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
pressKey(key: Key): void {
|
|
66
|
+
if (isModifier(key) && this.isPressed(key)) {
|
|
67
|
+
throw botError(UNKNOWN_ERROR, 'Cannot press a modifier key that is already pressed.')
|
|
68
|
+
}
|
|
69
|
+
const repeat = this.isPressed(key)
|
|
70
|
+
// A modifier reports itself as held on its own keydown.
|
|
71
|
+
this.pressed.add(key)
|
|
72
|
+
|
|
73
|
+
const target = this.target()
|
|
74
|
+
if (!this.fireKey(target, EventType.KEYDOWN, key, repeat)) {
|
|
75
|
+
return
|
|
76
|
+
}
|
|
77
|
+
if ((key.character || key === Keys.ENTER) && !this.fireKey(target, EventType.KEYPRESS, key, repeat)) {
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
// A file input's value cannot be set from script; uploads are the
|
|
81
|
+
// driver's job, not the keyboard's.
|
|
82
|
+
if (isEditable(target) && !isFileInput(target)) {
|
|
83
|
+
this.edit(target, key)
|
|
84
|
+
}
|
|
85
|
+
if (key === Keys.ENTER && isElement(target, 'INPUT')) {
|
|
86
|
+
implicitlySubmit(target as HTMLInputElement)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
releaseKey(key: Key): void {
|
|
91
|
+
if (!this.isPressed(key)) {
|
|
92
|
+
throw botError(UNKNOWN_ERROR, 'Cannot release a key that is not pressed. (' + key.key + ')')
|
|
93
|
+
}
|
|
94
|
+
// ...and as released on its own keyup.
|
|
95
|
+
this.pressed.delete(key)
|
|
96
|
+
this.fireKey(this.target(), EventType.KEYUP, key, false)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
releaseModifiers(): void {
|
|
100
|
+
for (const key of [Keys.ALT, Keys.CONTROL, Keys.META, Keys.SHIFT]) {
|
|
101
|
+
if (this.isPressed(key)) {
|
|
102
|
+
this.releaseKey(key)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Key events go where focus is, as they do for a real keyboard, falling back
|
|
108
|
+
// to the element being typed into when focus is nowhere in particular.
|
|
109
|
+
private target(): Element {
|
|
110
|
+
const element = this.element as Element
|
|
111
|
+
const active = getActiveElementFrom(element)
|
|
112
|
+
return active && !isElement(active, 'BODY') ? active : element
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private typed(key: Key): string {
|
|
116
|
+
return (this.isPressed(Keys.SHIFT) ? key.shiftCharacter : key.character) as string
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private fireKey(target: Element, type: EventFactory<KeyboardArgs>, key: Key, repeat: boolean): boolean {
|
|
120
|
+
const win = target.ownerDocument.defaultView || window
|
|
121
|
+
let keyValue = key.key
|
|
122
|
+
let keyCode = keyCodeFor(key, win)
|
|
123
|
+
let charCode = 0
|
|
124
|
+
if (key.character) {
|
|
125
|
+
keyValue = this.typed(key)
|
|
126
|
+
}
|
|
127
|
+
// keypress carries the typed character's code on both fields, in Chrome
|
|
128
|
+
// and in Firefox (which used to put it on charCode alone).
|
|
129
|
+
if (type === EventType.KEYPRESS) {
|
|
130
|
+
keyCode = charCode = key.character ? (keyValue.codePointAt(0) as number) : key.keyCode
|
|
131
|
+
}
|
|
132
|
+
return fire(target, type, {
|
|
133
|
+
key: keyValue,
|
|
134
|
+
code: key.code,
|
|
135
|
+
keyCode,
|
|
136
|
+
charCode,
|
|
137
|
+
location: key.location,
|
|
138
|
+
repeat,
|
|
139
|
+
altKey: this.isPressed(Keys.ALT),
|
|
140
|
+
ctrlKey: this.isPressed(Keys.CONTROL),
|
|
141
|
+
metaKey: this.isPressed(Keys.META) || this.isPressed(Keys.META_RIGHT),
|
|
142
|
+
shiftKey: this.isPressed(Keys.SHIFT),
|
|
143
|
+
})
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private edit(target: Element, key: Key): void {
|
|
147
|
+
const shift = this.isPressed(Keys.SHIFT)
|
|
148
|
+
if (isContentEditable(target)) {
|
|
149
|
+
editContent(target, key, key.character ? this.typed(key) : null, shift)
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
const field = target as HTMLInputElement | HTMLTextAreaElement
|
|
153
|
+
if (key.character) {
|
|
154
|
+
insertText(field, this.typed(key), 'insertText')
|
|
155
|
+
} else if (key === Keys.ENTER) {
|
|
156
|
+
if (isElement(field, 'TEXTAREA')) {
|
|
157
|
+
insertText(field, '\n', 'insertLineBreak')
|
|
158
|
+
} else {
|
|
159
|
+
// A single-line field takes no line break, but still announces one.
|
|
160
|
+
fire(field, EventType.BEFOREINPUT, { inputType: 'insertLineBreak', data: null })
|
|
161
|
+
}
|
|
162
|
+
} else if (key === Keys.BACKSPACE || key === Keys.DELETE) {
|
|
163
|
+
deleteText(field, key === Keys.DELETE)
|
|
164
|
+
} else if (key === Keys.LEFT || key === Keys.RIGHT || key === Keys.HOME || key === Keys.END) {
|
|
165
|
+
moveCaret(field, key, shift)
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
type TextField = HTMLInputElement | HTMLTextAreaElement
|
|
171
|
+
|
|
172
|
+
// email, number and the date-like types have a value but no selection API
|
|
173
|
+
// (selectionStart is null). Edits there go at the end, which is where the
|
|
174
|
+
// caret is after moveCursor.
|
|
175
|
+
function supportsSelection(field: TextField): boolean {
|
|
176
|
+
try {
|
|
177
|
+
return typeof field.selectionStart === 'number'
|
|
178
|
+
} catch (_unsupported) {
|
|
179
|
+
return false
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function caretToEnd(element: Element): void {
|
|
184
|
+
if (isContentEditable(element)) {
|
|
185
|
+
const selection = element.ownerDocument.getSelection()
|
|
186
|
+
if (selection) {
|
|
187
|
+
selection.selectAllChildren(element)
|
|
188
|
+
selection.collapseToEnd()
|
|
189
|
+
}
|
|
190
|
+
return
|
|
191
|
+
}
|
|
192
|
+
const field = element as TextField
|
|
193
|
+
if (supportsSelection(field)) {
|
|
194
|
+
field.setSelectionRange(field.value.length, field.value.length)
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function selectionOf(field: TextField): [number, number] {
|
|
199
|
+
if (supportsSelection(field)) {
|
|
200
|
+
return [field.selectionStart as number, field.selectionEnd as number]
|
|
201
|
+
}
|
|
202
|
+
return [field.value.length, field.value.length]
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function replaceRange(field: TextField, text: string, start: number, end: number): void {
|
|
206
|
+
if (supportsSelection(field)) {
|
|
207
|
+
field.setRangeText(text, start, end, 'end')
|
|
208
|
+
} else {
|
|
209
|
+
field.value = field.value.slice(0, start) + text + field.value.slice(end)
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// beforeinput, textInput, the edit, input -- the order both engines use.
|
|
214
|
+
// setRangeText fires no input event of its own, so it is fired here.
|
|
215
|
+
function insertText(field: TextField, text: string, inputType: string): void {
|
|
216
|
+
const data = inputType === 'insertText' ? text : null
|
|
217
|
+
if (!fire(field, EventType.BEFOREINPUT, { inputType, data })) {
|
|
218
|
+
return
|
|
219
|
+
}
|
|
220
|
+
if (inputType === 'insertText' && !fire(field, EventType.TEXTINPUT, { data: text })) {
|
|
221
|
+
return
|
|
222
|
+
}
|
|
223
|
+
const [start, end] = selectionOf(field)
|
|
224
|
+
if (field.maxLength >= 0 && field.value.length - (end - start) + text.length > field.maxLength) {
|
|
225
|
+
return
|
|
226
|
+
}
|
|
227
|
+
replaceRange(field, text, start, end)
|
|
228
|
+
fire(field, EventType.INPUT, { inputType, data })
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// One character, or a whole surrogate pair, before or after a collapsed
|
|
232
|
+
// caret; the selection when there is one. Nothing to delete means no events,
|
|
233
|
+
// as with a real Backspace at the start of a field.
|
|
234
|
+
function deleteText(field: TextField, forward: boolean): void {
|
|
235
|
+
let [start, end] = selectionOf(field)
|
|
236
|
+
const value = field.value
|
|
237
|
+
if (start === end) {
|
|
238
|
+
if (forward) {
|
|
239
|
+
if (end >= value.length) {
|
|
240
|
+
return
|
|
241
|
+
}
|
|
242
|
+
end = stepRight(value, end)
|
|
243
|
+
} else {
|
|
244
|
+
if (start <= 0) {
|
|
245
|
+
return
|
|
246
|
+
}
|
|
247
|
+
start = stepLeft(value, start)
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
const inputType = forward ? 'deleteContentForward' : 'deleteContentBackward'
|
|
251
|
+
if (!fire(field, EventType.BEFOREINPUT, { inputType, data: null })) {
|
|
252
|
+
return
|
|
253
|
+
}
|
|
254
|
+
replaceRange(field, '', start, end)
|
|
255
|
+
fire(field, EventType.INPUT, { inputType, data: null })
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// One character left or right of `index`, never splitting a surrogate pair.
|
|
259
|
+
function stepLeft(value: string, index: number): number {
|
|
260
|
+
return Math.max(index - (isSurrogatePairAt(value, index - 2) ? 2 : 1), 0)
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function stepRight(value: string, index: number): number {
|
|
264
|
+
return Math.min(index + (isSurrogatePairAt(value, index) ? 2 : 1), value.length)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function isSurrogatePairAt(value: string, index: number): boolean {
|
|
268
|
+
if (index < 0 || index + 1 >= value.length) {
|
|
269
|
+
return false
|
|
270
|
+
}
|
|
271
|
+
const high = value.charCodeAt(index)
|
|
272
|
+
const low = value.charCodeAt(index + 1)
|
|
273
|
+
return high >= 0xd800 && high <= 0xdbff && low >= 0xdc00 && low <= 0xdfff
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// Arrow, Home and End, with shift extending from the selection's anchor the
|
|
277
|
+
// way a real field does. The anchor is whichever end the selection direction
|
|
278
|
+
// says is not moving. Up and Down are not handled: they move by line, which a
|
|
279
|
+
// field's selection API cannot express.
|
|
280
|
+
function moveCaret(field: TextField, key: Key, shift: boolean): void {
|
|
281
|
+
if (!supportsSelection(field)) {
|
|
282
|
+
return
|
|
283
|
+
}
|
|
284
|
+
const [start, end] = selectionOf(field)
|
|
285
|
+
const length = field.value.length
|
|
286
|
+
const backward = field.selectionDirection === 'backward'
|
|
287
|
+
const anchor = backward ? end : start
|
|
288
|
+
const focus = backward ? start : end
|
|
289
|
+
|
|
290
|
+
if (!shift) {
|
|
291
|
+
let caret: number
|
|
292
|
+
if (key === Keys.HOME) {
|
|
293
|
+
caret = 0
|
|
294
|
+
} else if (key === Keys.END) {
|
|
295
|
+
caret = length
|
|
296
|
+
} else if (start !== end) {
|
|
297
|
+
caret = key === Keys.LEFT ? start : end
|
|
298
|
+
} else {
|
|
299
|
+
caret = key === Keys.LEFT ? stepLeft(field.value, start) : stepRight(field.value, end)
|
|
300
|
+
}
|
|
301
|
+
field.setSelectionRange(caret, caret, 'none')
|
|
302
|
+
return
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
let moved: number
|
|
306
|
+
if (key === Keys.HOME) {
|
|
307
|
+
moved = 0
|
|
308
|
+
} else if (key === Keys.END) {
|
|
309
|
+
moved = length
|
|
310
|
+
} else if (key === Keys.LEFT) {
|
|
311
|
+
moved = stepLeft(field.value, focus)
|
|
312
|
+
} else {
|
|
313
|
+
moved = stepRight(field.value, focus)
|
|
314
|
+
}
|
|
315
|
+
// A selection with no prior extent is anchored at the caret.
|
|
316
|
+
const fixed = start === end ? start : anchor
|
|
317
|
+
field.setSelectionRange(Math.min(fixed, moved), Math.max(fixed, moved), moved < fixed ? 'backward' : 'forward')
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// contenteditable edits go through execCommand: it is deprecated, but it is
|
|
321
|
+
// the one way a script can edit rich content that keeps the browser's undo
|
|
322
|
+
// stack, normalises the markup the way typing would, and fires a trusted
|
|
323
|
+
// input event with the right inputType. It does not fire beforeinput, so
|
|
324
|
+
// that is fired first and honoured here.
|
|
325
|
+
function editContent(target: Element, key: Key, typed: string | null, shift: boolean): void {
|
|
326
|
+
const doc = target.ownerDocument
|
|
327
|
+
const selection = doc.getSelection()
|
|
328
|
+
if (selection && !(selection.anchorNode && target.contains(selection.anchorNode))) {
|
|
329
|
+
selection.selectAllChildren(target)
|
|
330
|
+
selection.collapseToEnd()
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (typed !== null) {
|
|
334
|
+
if (!fire(target, EventType.BEFOREINPUT, { inputType: 'insertText', data: typed })) {
|
|
335
|
+
return
|
|
336
|
+
}
|
|
337
|
+
if (!fire(target, EventType.TEXTINPUT, { data: typed })) {
|
|
338
|
+
return
|
|
339
|
+
}
|
|
340
|
+
doc.execCommand('insertText', false, typed)
|
|
341
|
+
return
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const commands: Array<[Key, string, string]> = [
|
|
345
|
+
[Keys.ENTER, 'insertParagraph', 'insertParagraph'],
|
|
346
|
+
[Keys.BACKSPACE, 'deleteContentBackward', 'delete'],
|
|
347
|
+
[Keys.DELETE, 'deleteContentForward', 'forwardDelete'],
|
|
348
|
+
]
|
|
349
|
+
const command = commands.find(([k]) => k === key)
|
|
350
|
+
if (command) {
|
|
351
|
+
if (fire(target, EventType.BEFOREINPUT, { inputType: command[1], data: null })) {
|
|
352
|
+
doc.execCommand(command[2], false)
|
|
353
|
+
}
|
|
354
|
+
return
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// Selection.modify is non-standard but present in every supported engine;
|
|
358
|
+
// it is the only API that moves a caret by character or line boundary.
|
|
359
|
+
const modify = selection && (selection as Selection & { modify?: Function }).modify
|
|
360
|
+
if (!modify) {
|
|
361
|
+
return
|
|
362
|
+
}
|
|
363
|
+
const alter = shift ? 'extend' : 'move'
|
|
364
|
+
if (key === Keys.LEFT || key === Keys.RIGHT) {
|
|
365
|
+
modify.call(selection, alter, key === Keys.LEFT ? 'backward' : 'forward', 'character')
|
|
366
|
+
} else if (key === Keys.HOME || key === Keys.END) {
|
|
367
|
+
modify.call(selection, alter, key === Keys.HOME ? 'backward' : 'forward', 'lineboundary')
|
|
368
|
+
}
|
|
369
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// Licensed to the Software Freedom Conservancy (SFC) under one
|
|
2
|
+
// or more contributor license agreements. See the NOTICE file
|
|
3
|
+
// distributed with this work for additional information
|
|
4
|
+
// regarding copyright ownership. The SFC licenses this file
|
|
5
|
+
// to you under the Apache License, Version 2.0 (the
|
|
6
|
+
// "License"); you may not use this file except in compliance
|
|
7
|
+
// with the License. You may obtain a copy of the License at
|
|
8
|
+
//
|
|
9
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
//
|
|
11
|
+
// Unless required by applicable law or agreed to in writing,
|
|
12
|
+
// software distributed under the License is distributed on an
|
|
13
|
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
+
// KIND, either express or implied. See the License for the
|
|
15
|
+
// specific language governing permissions and limitations
|
|
16
|
+
// under the License.
|
|
17
|
+
|
|
18
|
+
import { isGecko, isMac } from './useragent.js'
|
|
19
|
+
|
|
20
|
+
// The keys the keyboard knows, each carrying what a real press reports: the
|
|
21
|
+
// `key` and `code` values, the legacy keyCode for keydown / keyup, and for
|
|
22
|
+
// character keys the character typed without and with shift.
|
|
23
|
+
//
|
|
24
|
+
// keyCodes match what Chrome and Firefox report for real presses (recorded
|
|
25
|
+
// 2026-10-13). They agree except for '=', '-', ';' and Meta, where Firefox has
|
|
26
|
+
// its own numbers; `gecko` carries those.
|
|
27
|
+
|
|
28
|
+
export const LOCATION_STANDARD = 0
|
|
29
|
+
export const LOCATION_LEFT = 1
|
|
30
|
+
export const LOCATION_RIGHT = 2
|
|
31
|
+
export const LOCATION_NUMPAD = 3
|
|
32
|
+
|
|
33
|
+
export interface Key {
|
|
34
|
+
// The key value for a non-character key ('Enter', 'ArrowLeft').
|
|
35
|
+
readonly key: string
|
|
36
|
+
readonly code: string
|
|
37
|
+
readonly keyCode: number
|
|
38
|
+
readonly gecko?: number
|
|
39
|
+
readonly location: number
|
|
40
|
+
readonly character: string | null
|
|
41
|
+
readonly shiftCharacter: string | null
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function key(
|
|
45
|
+
keyValue: string,
|
|
46
|
+
code: string,
|
|
47
|
+
keyCode: number,
|
|
48
|
+
options: { gecko?: number; location?: number; character?: string; shiftCharacter?: string } = {},
|
|
49
|
+
): Key {
|
|
50
|
+
return {
|
|
51
|
+
key: keyValue,
|
|
52
|
+
code,
|
|
53
|
+
keyCode,
|
|
54
|
+
gecko: options.gecko,
|
|
55
|
+
location: options.location ?? LOCATION_STANDARD,
|
|
56
|
+
character: options.character ?? null,
|
|
57
|
+
shiftCharacter: options.shiftCharacter ?? options.character ?? null,
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function character(code: string, keyCode: number, ch: string, shifted?: string, gecko?: number): Key {
|
|
62
|
+
return key(ch, code, keyCode, { character: ch, shiftCharacter: shifted, gecko })
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function numpad(code: string, keyCode: number, ch: string): Key {
|
|
66
|
+
return key(ch, code, keyCode, { character: ch, location: LOCATION_NUMPAD })
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function letters(): Record<string, Key> {
|
|
70
|
+
const result: Record<string, Key> = {}
|
|
71
|
+
for (let i = 0; i < 26; i++) {
|
|
72
|
+
const upper = String.fromCharCode(65 + i)
|
|
73
|
+
result[upper] = character('Key' + upper, 65 + i, upper.toLowerCase(), upper)
|
|
74
|
+
}
|
|
75
|
+
return result
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function functionKeys(): Record<string, Key> {
|
|
79
|
+
const result: Record<string, Key> = {}
|
|
80
|
+
for (let i = 1; i <= 12; i++) {
|
|
81
|
+
result['F' + i] = key('F' + i, 'F' + i, 111 + i)
|
|
82
|
+
}
|
|
83
|
+
return result
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const DIGIT_SHIFTS = ')!@#$%^&*('
|
|
87
|
+
|
|
88
|
+
function digits(): Record<string, Key> {
|
|
89
|
+
const names = ['ZERO', 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE']
|
|
90
|
+
const result: Record<string, Key> = {}
|
|
91
|
+
names.forEach((name, i) => {
|
|
92
|
+
result[name] = character('Digit' + i, 48 + i, String(i), DIGIT_SHIFTS[i])
|
|
93
|
+
result['NUM_' + name] = numpad('Numpad' + i, 96 + i, String(i))
|
|
94
|
+
})
|
|
95
|
+
return result
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export const Keys = {
|
|
99
|
+
BACKSPACE: key('Backspace', 'Backspace', 8),
|
|
100
|
+
TAB: key('Tab', 'Tab', 9),
|
|
101
|
+
ENTER: key('Enter', 'Enter', 13),
|
|
102
|
+
SHIFT: key('Shift', 'ShiftLeft', 16, { location: LOCATION_LEFT }),
|
|
103
|
+
CONTROL: key('Control', 'ControlLeft', 17, { location: LOCATION_LEFT }),
|
|
104
|
+
ALT: key('Alt', 'AltLeft', 18, { location: LOCATION_LEFT }),
|
|
105
|
+
PAUSE: key('Pause', 'Pause', 19),
|
|
106
|
+
CAPS_LOCK: key('CapsLock', 'CapsLock', 20),
|
|
107
|
+
ESC: key('Escape', 'Escape', 27),
|
|
108
|
+
SPACE: character('Space', 32, ' '),
|
|
109
|
+
PAGE_UP: key('PageUp', 'PageUp', 33),
|
|
110
|
+
PAGE_DOWN: key('PageDown', 'PageDown', 34),
|
|
111
|
+
END: key('End', 'End', 35),
|
|
112
|
+
HOME: key('Home', 'Home', 36),
|
|
113
|
+
LEFT: key('ArrowLeft', 'ArrowLeft', 37),
|
|
114
|
+
UP: key('ArrowUp', 'ArrowUp', 38),
|
|
115
|
+
RIGHT: key('ArrowRight', 'ArrowRight', 39),
|
|
116
|
+
DOWN: key('ArrowDown', 'ArrowDown', 40),
|
|
117
|
+
PRINT_SCREEN: key('PrintScreen', 'PrintScreen', 44),
|
|
118
|
+
INSERT: key('Insert', 'Insert', 45),
|
|
119
|
+
DELETE: key('Delete', 'Delete', 46),
|
|
120
|
+
|
|
121
|
+
...digits(),
|
|
122
|
+
...letters(),
|
|
123
|
+
|
|
124
|
+
// Firefox on macOS reports 224 for both Meta keys; every other combination
|
|
125
|
+
// reports the Windows-style 91 / 92. Firefox on Linux is not verified.
|
|
126
|
+
META: key('Meta', 'MetaLeft', 91, { gecko: 91, location: LOCATION_LEFT }),
|
|
127
|
+
META_RIGHT: key('Meta', 'MetaRight', 92, { gecko: 92, location: LOCATION_RIGHT }),
|
|
128
|
+
CONTEXT_MENU: key('ContextMenu', 'ContextMenu', 93),
|
|
129
|
+
|
|
130
|
+
NUM_MULTIPLY: numpad('NumpadMultiply', 106, '*'),
|
|
131
|
+
NUM_PLUS: numpad('NumpadAdd', 107, '+'),
|
|
132
|
+
NUM_MINUS: numpad('NumpadSubtract', 109, '-'),
|
|
133
|
+
NUM_PERIOD: numpad('NumpadDecimal', 110, '.'),
|
|
134
|
+
NUM_DIVISION: numpad('NumpadDivide', 111, '/'),
|
|
135
|
+
NUM_LOCK: key('NumLock', 'NumLock', 144, { location: LOCATION_NUMPAD }),
|
|
136
|
+
SEPARATOR: numpad('NumpadComma', 108, ','),
|
|
137
|
+
|
|
138
|
+
...functionKeys(),
|
|
139
|
+
|
|
140
|
+
EQUALS: character('Equal', 187, '=', '+', 61),
|
|
141
|
+
HYPHEN: character('Minus', 189, '-', '_', 173),
|
|
142
|
+
COMMA: character('Comma', 188, ',', '<'),
|
|
143
|
+
PERIOD: character('Period', 190, '.', '>'),
|
|
144
|
+
SLASH: character('Slash', 191, '/', '?'),
|
|
145
|
+
BACKTICK: character('Backquote', 192, '`', '~'),
|
|
146
|
+
OPEN_BRACKET: character('BracketLeft', 219, '[', '{'),
|
|
147
|
+
BACKSLASH: character('Backslash', 220, '\\', '|'),
|
|
148
|
+
CLOSE_BRACKET: character('BracketRight', 221, ']', '}'),
|
|
149
|
+
SEMICOLON: character('Semicolon', 186, ';', ':', 59),
|
|
150
|
+
APOSTROPHE: character('Quote', 222, "'", '"'),
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export const MODIFIERS: readonly Key[] = [Keys.ALT, Keys.CONTROL, Keys.META, Keys.SHIFT]
|
|
154
|
+
|
|
155
|
+
export function isModifier(k: Key): boolean {
|
|
156
|
+
return MODIFIERS.includes(k)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// The keyCode a keydown or keyup for this key reports in the given window's
|
|
160
|
+
// engine.
|
|
161
|
+
export function keyCodeFor(k: Key, win: Window): number {
|
|
162
|
+
if (!isGecko(win)) {
|
|
163
|
+
if (k === Keys.META_RIGHT && isMac(win)) {
|
|
164
|
+
return 93
|
|
165
|
+
}
|
|
166
|
+
return k.keyCode
|
|
167
|
+
}
|
|
168
|
+
if ((k === Keys.META || k === Keys.META_RIGHT) && isMac(win)) {
|
|
169
|
+
return 224
|
|
170
|
+
}
|
|
171
|
+
return k.gecko ?? k.keyCode
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Character to (key, needs shift). Numpad keys never win: a typed '1' is the
|
|
175
|
+
// digit row's 1, as on a real keyboard without a numpad.
|
|
176
|
+
const CHAR_TO_KEY = new Map<string, { key: Key; shift: boolean }>()
|
|
177
|
+
for (const k of Object.values(Keys) as Key[]) {
|
|
178
|
+
if (!k.character || k.location === LOCATION_NUMPAD) {
|
|
179
|
+
continue
|
|
180
|
+
}
|
|
181
|
+
if (!CHAR_TO_KEY.has(k.character)) {
|
|
182
|
+
CHAR_TO_KEY.set(k.character, { key: k, shift: false })
|
|
183
|
+
}
|
|
184
|
+
if (k.shiftCharacter && k.shiftCharacter !== k.character) {
|
|
185
|
+
CHAR_TO_KEY.set(k.shiftCharacter, { key: k, shift: true })
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// A key that types `ch`, a single code point. For characters not on a US
|
|
190
|
+
// layout there is no true keyCode to report; the upper-case code point is
|
|
191
|
+
// sometimes right and at least positive, which is what bot.Keyboard did.
|
|
192
|
+
export function fromChar(ch: string): { key: Key; shift: boolean } {
|
|
193
|
+
if (Array.from(ch).length !== 1) {
|
|
194
|
+
throw new Error('Argument not a single character: ' + ch)
|
|
195
|
+
}
|
|
196
|
+
const known = CHAR_TO_KEY.get(ch)
|
|
197
|
+
if (known) {
|
|
198
|
+
return known
|
|
199
|
+
}
|
|
200
|
+
const upper = ch.toUpperCase()
|
|
201
|
+
const lower = ch.toLowerCase()
|
|
202
|
+
const k = key(ch, '', upper.codePointAt(0) as number, { character: lower, shiftCharacter: upper })
|
|
203
|
+
return { key: k, shift: ch !== lower }
|
|
204
|
+
}
|