Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/neteditTestFunctions/input/mouse.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 mouse.py
15
# @author Pablo Alvarez Lopez
16
# @date 28-05-25
17
18
# imports
19
import time
20
import pyautogui
21
from ..constants import DELAY_MOUSE_MOVE, DELAY_MOUSE_CLICK, DELAY_DRAGDROP, DELAY_KEY
22
from ..input.keyboard import keyPress, keyRelease, typeKey
23
24
25
def leftClick(referencePosition, position):
26
"""
27
@brief do left click over a position relative to referencePosition (pink square)
28
"""
29
# obtain clicked position
30
clickedPosition = [referencePosition[0] + position.x, referencePosition[1] + position.y]
31
# move mouse to position
32
pyautogui.moveTo(clickedPosition)
33
# wait after move
34
time.sleep(DELAY_MOUSE_MOVE)
35
# click over position
36
pyautogui.click(button='left')
37
# wait after every operation
38
time.sleep(DELAY_MOUSE_CLICK)
39
# show debug
40
print("TestFunctions: Clicked over position", clickedPosition[0], '-', clickedPosition[1])
41
42
43
def leftClickData(referencePosition, position):
44
"""
45
@brief do left click over a position relative to referencePosition (pink square) with an small offest y
46
"""
47
# obtain clicked position
48
clickedPosition = [referencePosition[0] + position.x, referencePosition[1] + position.y + 30]
49
# move mouse to position
50
pyautogui.moveTo(clickedPosition)
51
# wait after move
52
time.sleep(DELAY_MOUSE_MOVE)
53
# click over position
54
pyautogui.click(button='left')
55
# wait after every operation
56
time.sleep(DELAY_MOUSE_CLICK)
57
# show debug (rest 30 to y position to unify output with internal tests)
58
print("TestFunctions: Clicked over position", clickedPosition[0], '-', clickedPosition[1] - 30)
59
60
61
def leftClickOffset(referencePosition, position, offsetX, offsetY):
62
"""
63
@brief do left click over a position relative to referencePosition (pink square)
64
"""
65
# obtain clicked position
66
clickedPosition = [referencePosition[0] + position.x + offsetX, referencePosition[1] + position.y + offsetY]
67
# move mouse to position
68
pyautogui.moveTo(clickedPosition)
69
# wait after move
70
time.sleep(DELAY_MOUSE_MOVE)
71
# click over position
72
pyautogui.click(button='left')
73
# wait after every operation
74
time.sleep(DELAY_MOUSE_CLICK)
75
# show debug
76
print("TestFunctions: Clicked over position", clickedPosition[0], '-', clickedPosition[1])
77
78
79
def leftClickShift(referencePosition, position):
80
"""
81
@brief do left click over a position relative to referencePosition (pink square) while shift key is pressed
82
"""
83
# Leave Shift key pressed
84
keyPress('shift')
85
# obtain clicked position
86
clickedPosition = [referencePosition[0] + position.x, referencePosition[1] + position.y]
87
# move mouse to position
88
pyautogui.moveTo(clickedPosition)
89
# wait after move
90
time.sleep(DELAY_MOUSE_MOVE)
91
# click over position
92
pyautogui.click(button='left')
93
# wait after every operation
94
time.sleep(DELAY_MOUSE_CLICK)
95
# show debug
96
print("TestFunctions: Clicked with Shift key pressed over position", clickedPosition[0], '-', clickedPosition[1])
97
# Release Shift key
98
keyRelease('shift')
99
100
101
def leftClickControl(referencePosition, position):
102
"""
103
@brief do left click over a position relative to referencePosition (pink square) while control key is pressed
104
"""
105
# Leave Control key pressed
106
keyPress('ctrl')
107
# obtain clicked position
108
clickedPosition = [referencePosition[0] + position.x, referencePosition[1] + position.y]
109
# move mouse to position
110
pyautogui.moveTo(clickedPosition)
111
# wait after move
112
time.sleep(DELAY_MOUSE_MOVE)
113
# click over position
114
pyautogui.click(button='left')
115
# wait after every operation
116
time.sleep(DELAY_MOUSE_CLICK)
117
# show debug
118
print("TestFunctions: Clicked with Control key pressed over position", clickedPosition[0], '-', clickedPosition[1])
119
# Release Control key
120
keyRelease('ctrl')
121
122
123
def leftClickAltShift(referencePosition, position):
124
"""
125
@brief do left click over a position relative to referencePosition (pink square) while alt key is pressed
126
"""
127
# Leave alt key pressed
128
keyPress('alt')
129
# Leave shift key pressed
130
keyPress('shift')
131
# obtain clicked position
132
clickedPosition = [referencePosition[0] + position.x, referencePosition[1] + position.y]
133
# move mouse to position
134
pyautogui.moveTo(clickedPosition)
135
# wait after move
136
time.sleep(DELAY_MOUSE_MOVE)
137
# click over position
138
pyautogui.click(button='left')
139
# wait after every operation
140
time.sleep(DELAY_MOUSE_CLICK)
141
# show debug
142
print("TestFunctions: Clicked with alt and shift key pressed over position",
143
clickedPosition[0], '-', clickedPosition[1])
144
# Release alt key
145
keyRelease('alt')
146
# Release shift key
147
keyRelease('shift')
148
149
150
def rightClick(referencePosition, position, offsetX=0, offsetY=0):
151
"""
152
@brief do right click over a position relative to referencePosition (pink square)
153
"""
154
# obtain clicked position
155
clickedPosition = [referencePosition[0] + position.x + offsetX, referencePosition[1] + position.y + offsetY]
156
# move mouse to position
157
pyautogui.moveTo(clickedPosition)
158
# wait after move
159
time.sleep(DELAY_MOUSE_MOVE)
160
# click over position
161
pyautogui.click(button='right')
162
# wait after every operation
163
time.sleep(DELAY_MOUSE_CLICK)
164
# show debug
165
print("TestFunctions: Clicked over position", clickedPosition[0], '-', clickedPosition[1])
166
167
168
def leftClickMultiElement(referencePosition, position, underElement, offsetX=0, offsetY=0):
169
"""
170
@brief do left click over a position relative to referencePosition (pink square) and selecting under element
171
"""
172
# obtain clicked position
173
clickedPosition = [referencePosition[0] + position.x + offsetX, referencePosition[1] + position.y + offsetY]
174
# move mouse to position
175
pyautogui.moveTo(clickedPosition)
176
# wait after move
177
time.sleep(DELAY_MOUSE_MOVE)
178
# click over position
179
pyautogui.click(button='left')
180
# wait after every operation
181
time.sleep(DELAY_MOUSE_CLICK)
182
# go to element
183
for _ in range(underElement + 1):
184
typeKey('down')
185
typeKey('space')
186
print("TestFunctions: Clicked over position",
187
clickedPosition[0], '-', clickedPosition[1], "under element", underElement)
188
189
190
def moveMouse(referencePosition, position, offsetX=0, offsetY=0, report=True):
191
"""
192
@brief move mouse to the given position
193
"""
194
# obtain clicked position
195
movePosition = [referencePosition[0] + position.x + offsetX, referencePosition[1] + position.y + offsetY]
196
# move mouse to position
197
pyautogui.moveTo(movePosition)
198
# wait after move
199
time.sleep(DELAY_MOUSE_MOVE)
200
# show debug
201
if (report):
202
print("TestFunctions: Moved to position", movePosition[0], '-', movePosition[1])
203
204
205
def dragDrop(referencePosition, x1, y1, x2, y2):
206
"""
207
@brief drag and drop from position 1 to position 2
208
"""
209
# wait before every operation
210
time.sleep(DELAY_KEY)
211
# obtain from and to position
212
fromPosition = [referencePosition[0] + x1, referencePosition[1] + y1]
213
tromPosition = [referencePosition[0] + x2, referencePosition[1] + y2]
214
# move to from position
215
pyautogui.moveTo(fromPosition)
216
# wait before every operation
217
time.sleep(DELAY_KEY)
218
# drag mouse to X of 100, Y of 200 while holding down left mouse button
219
pyautogui.dragTo(tromPosition[0], tromPosition[1], DELAY_DRAGDROP, button='left')
220
# wait before every operation
221
time.sleep(DELAY_KEY)
222
223