Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/common/actions/key_input.py
1864 views
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
from . import interaction
18
from .input_device import InputDevice
19
from .interaction import Interaction, Pause
20
21
22
class KeyInput(InputDevice):
23
def __init__(self, name: str) -> None:
24
super().__init__()
25
self.name = name
26
self.type = interaction.KEY
27
28
def encode(self) -> dict:
29
return {"type": self.type, "id": self.name, "actions": [acts.encode() for acts in self.actions]}
30
31
def create_key_down(self, key) -> None:
32
self.add_action(TypingInteraction(self, "keyDown", key))
33
34
def create_key_up(self, key) -> None:
35
self.add_action(TypingInteraction(self, "keyUp", key))
36
37
def create_pause(self, pause_duration: float = 0) -> None:
38
self.add_action(Pause(self, pause_duration))
39
40
41
class TypingInteraction(Interaction):
42
def __init__(self, source, type_, key) -> None:
43
super().__init__(source)
44
self.type = type_
45
self.key = key
46
47
def encode(self) -> dict:
48
return {"type": self.type, "value": self.key}
49
50