Skip to content

Commit 5d70444

Browse files
committed
implement is_alt_clicked
1 parent 386e235 commit 5d70444

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

adafruit_cursorcontrol/cursorcontrol_cursormanager.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
PYBADGE_BUTTON_RIGHT = const(4)
3333
# PyBadge & PyGamer
3434
PYBADGE_BUTTON_A = const(1)
35+
PYBADGE_BUTTON_B = const(0)
3536

3637

3738
class CursorManager:
@@ -43,6 +44,7 @@ class CursorManager:
4344
def __init__(self, cursor: Cursor) -> None:
4445
self._cursor = cursor
4546
self._is_clicked = False
47+
self._is_alt_clicked = False
4648
self._pad_states = 0
4749
self._event = Event()
4850
self._init_hardware()
@@ -83,12 +85,13 @@ def _init_hardware(self) -> None:
8385
"btn_up": PYBADGE_BUTTON_UP,
8486
"btn_down": PYBADGE_BUTTON_DOWN,
8587
"btn_a": PYBADGE_BUTTON_A,
88+
"btn_b": PYBADGE_BUTTON_B,
8689
}
8790
self._pad_states = 0
8891
elif hasattr(board, "JOYSTICK_X"):
8992
self._joystick_x = analogio.AnalogIn(board.JOYSTICK_X)
9093
self._joystick_y = analogio.AnalogIn(board.JOYSTICK_Y)
91-
self._pad_btns = {"btn_a": PYBADGE_BUTTON_A}
94+
self._pad_btns = {"btn_a": PYBADGE_BUTTON_A, "btn_b": PYBADGE_BUTTON_B}
9295
# Sample the center points of the joystick
9396
self._center_x = self._joystick_x.value
9497
self._center_y = self._joystick_y.value
@@ -111,6 +114,13 @@ def is_clicked(self) -> bool:
111114
"""
112115
return self._is_clicked
113116

117+
@property
118+
def is_alt_clicked(self) -> bool:
119+
"""Returns True if the cursor button was pressed
120+
during previous call to update()
121+
"""
122+
return self._is_alt_clicked
123+
114124
def update(self) -> None:
115125
"""Updates the cursor object."""
116126
if self._pad.events.get_into(self._event):
@@ -121,6 +131,11 @@ def update(self) -> None:
121131
elif self._pad_states & (1 << self._pad_btns["btn_a"]):
122132
self._is_clicked = True
123133

134+
if self._is_alt_clicked:
135+
self._is_alt_clicked = False
136+
elif self._pad_states & (1 << self._pad_btns["btn_b"]):
137+
self._is_alt_clicked = True
138+
124139
def _read_joystick_x(self, samples: int = 3) -> float:
125140
"""Read the X analog joystick on the PyGamer.
126141
:param int samples: How many samples to read and average.

0 commit comments

Comments
 (0)