Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/neteditTestFunctions/input/keyboard.py
169679 views
1
# -*- coding: utf-8 -*-
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2009-2025 German Aerospace Center (DLR) and others.
4
# This program and the accompanying materials are made available under the
5
# terms of the Eclipse Public License 2.0 which is available at
6
# https://www.eclipse.org/legal/epl-2.0/
7
# This Source Code may also be made available under the following Secondary
8
# Licenses when the conditions for such availability set forth in the Eclipse
9
# Public License 2.0 are satisfied: GNU General Public License, version 2
10
# or later which is available at
11
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
14
# @file keyboard.py
15
# @author Pablo Alvarez Lopez
16
# @date 28-05-25
17
18
# imports
19
import pyautogui
20
import time
21
import pyperclip
22
from ..constants import DELAY_KEY
23
24
# Define the mapping from English to German keyboard layout
25
EN_KEYS = r"""y[];'\z/Y{}:"|Z<>?@#^&*()-_=+"""
26
DE_KEYS = u"""zü+öä#y-ZÜ*ÖħY,._"'^&()=ß?´¨"""
27
TRANS_TABLE = str.maketrans(EN_KEYS, DE_KEYS)
28
29
30
def typeKey(key):
31
"""
32
@brief type single key
33
"""
34
# type keys
35
pyautogui.hotkey(key)
36
# wait before every operation
37
time.sleep(DELAY_KEY)
38
39
40
def keyPress(key):
41
"""
42
@brief type single key press
43
"""
44
# Leave key down
45
pyautogui.keyDown(key)
46
# wait after key down
47
time.sleep(DELAY_KEY)
48
49
50
def keyRelease(key):
51
"""
52
@brief type single key release
53
"""
54
# Leave key up
55
pyautogui.keyUp(key)
56
# wait after key up
57
time.sleep(DELAY_KEY)
58
59
60
def typeTwoKeys(key1, key2):
61
"""
62
@brief type two keys at the same time (key1 -> key2)
63
"""
64
# release key 1
65
keyPress(key1)
66
# type key 2
67
typeKey(key2)
68
# release key 1
69
keyRelease(key1)
70
71
72
def typeThreeKeys(key1, key2, key3):
73
"""
74
@brief type three keys at the same time (key1 -> key2 -> key3)
75
"""
76
# press key 1
77
keyPress(key1)
78
# press key 1
79
keyPress(key2)
80
# type key 3
81
typeKey(key3)
82
# release key 2
83
keyRelease(key2)
84
# release key 1
85
keyRelease(key1)
86
87
88
def translateKeys(value, layout="de"):
89
"""
90
@brief translate keys between different keyboards
91
"""
92
if layout == "de":
93
return value.translate(TRANS_TABLE).encode("latin-1")
94
return value
95
96
97
def updateText(newText, removePreviousContents=True, useClipboard=True, layout="de"):
98
"""
99
@brief set the given new text in the focused textField/ComboBox/etc.
100
"""
101
print(newText)
102
# remove previous content
103
if removePreviousContents:
104
typeTwoKeys('ctrl', 'a')
105
if useClipboard:
106
# use copy & paste (due problems with certain characters, for example '|')
107
pyperclip.copy(newText)
108
pyautogui.hotkey('ctrl', 'v')
109
else:
110
pyautogui.typewrite(translateKeys(newText, layout))
111
112