-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathEvent.py
executable file
·60 lines (49 loc) · 2.19 KB
/
Event.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
# Copyright © 2012-13 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version. It is provided for
# educational purposes and is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
import random
import string
random.seed(917) # Don't want random for regression tests
MOUSE, KEYPRESS, TIMER, TERMINATE = ("MOUSE", "KEYPRESS", "TIMER",
"TERMINATE")
def next():
kinds = ([MOUSE] * 7) + ([KEYPRESS] * 11) + ([TIMER] * 5) + [TERMINATE]
kind = random.choice(kinds)
if kind == MOUSE:
return Event(kind, button=random.randint(1, 3),
x=random.randint(0, 640), y=random.randint(0, 480))
elif kind == KEYPRESS:
return Event(kind, ctrl=random.randint(1, 7) == 1,
shift=random.randint(1, 5) == 1,
key=random.choice(string.ascii_lowercase))
return Event(kind) # TIMER or TERMINATE
class Event:
TimerId = 0
def __init__(self, kind, **kwargs):
assert kind in {MOUSE, KEYPRESS, TIMER, TERMINATE}
self.kind = kind
self.kwargs = kwargs
if self.kind == TIMER:
self.kwargs["id"] = Event.TimerId
Event.TimerId += 1
def __str__(self):
if self.kind == MOUSE:
return "Button {} ({}, {})".format(
self.kwargs.get("button", 1), self.kwargs.get("x", -1),
self.kwargs.get("y", -1))
elif self.kind == KEYPRESS:
return "Key {}{}{}".format(
"Ctrl+" if self.kwargs.get("ctrl", False) else "",
"Shift+" if self.kwargs.get("shift", False) else "",
self.kwargs.get("key", ""))
elif self.kind == TIMER:
return "Timer {}".format(self.kwargs.get("id", -1))
elif self.kind == TERMINATE:
return "Terminate"