Path: blob/main/tools/neteditTestFunctions/input/keyboard.py
169679 views
# -*- coding: utf-8 -*-1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2# Copyright (C) 2009-2025 German Aerospace Center (DLR) and others.3# This program and the accompanying materials are made available under the4# terms of the Eclipse Public License 2.0 which is available at5# https://www.eclipse.org/legal/epl-2.0/6# This Source Code may also be made available under the following Secondary7# Licenses when the conditions for such availability set forth in the Eclipse8# Public License 2.0 are satisfied: GNU General Public License, version 29# or later which is available at10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1213# @file keyboard.py14# @author Pablo Alvarez Lopez15# @date 28-05-251617# imports18import pyautogui19import time20import pyperclip21from ..constants import DELAY_KEY2223# Define the mapping from English to German keyboard layout24EN_KEYS = r"""y[];'\z/Y{}:"|Z<>?@#^&*()-_=+"""25DE_KEYS = u"""zü+öä#y-ZÜ*ÖħY,._"'^&()=ß?´¨"""26TRANS_TABLE = str.maketrans(EN_KEYS, DE_KEYS)272829def typeKey(key):30"""31@brief type single key32"""33# type keys34pyautogui.hotkey(key)35# wait before every operation36time.sleep(DELAY_KEY)373839def keyPress(key):40"""41@brief type single key press42"""43# Leave key down44pyautogui.keyDown(key)45# wait after key down46time.sleep(DELAY_KEY)474849def keyRelease(key):50"""51@brief type single key release52"""53# Leave key up54pyautogui.keyUp(key)55# wait after key up56time.sleep(DELAY_KEY)575859def typeTwoKeys(key1, key2):60"""61@brief type two keys at the same time (key1 -> key2)62"""63# release key 164keyPress(key1)65# type key 266typeKey(key2)67# release key 168keyRelease(key1)697071def typeThreeKeys(key1, key2, key3):72"""73@brief type three keys at the same time (key1 -> key2 -> key3)74"""75# press key 176keyPress(key1)77# press key 178keyPress(key2)79# type key 380typeKey(key3)81# release key 282keyRelease(key2)83# release key 184keyRelease(key1)858687def translateKeys(value, layout="de"):88"""89@brief translate keys between different keyboards90"""91if layout == "de":92return value.translate(TRANS_TABLE).encode("latin-1")93return value949596def updateText(newText, removePreviousContents=True, useClipboard=True, layout="de"):97"""98@brief set the given new text in the focused textField/ComboBox/etc.99"""100print(newText)101# remove previous content102if removePreviousContents:103typeTwoKeys('ctrl', 'a')104if useClipboard:105# use copy & paste (due problems with certain characters, for example '|')106pyperclip.copy(newText)107pyautogui.hotkey('ctrl', 'v')108else:109pyautogui.typewrite(translateKeys(newText, layout))110111112