Created
November 10, 2022 20:15
-
-
Save adamberenzweig/25d71d97ca7c1510fefd5a662791f576 to your computer and use it in GitHub Desktop.
test_DarwinTrackpadListener.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Quartz | |
import time | |
from AppKit import NSApplication, NSEvent, NSWorkspace | |
from pynput import _logger | |
from pynput._util.darwin import ListenerMixin | |
from pynput._util import AbstractListener, prefix | |
class DarwinTrackpadListener(ListenerMixin, AbstractListener): | |
_EVENTS = ( | |
Quartz.CGEventMaskBit(Quartz.NSEventTypeGesture) | |
# This works too: | |
# Quartz.kCGEventMaskForAllEvents | |
) | |
def __init__(self, *args, **kwargs): | |
self._log = _logger(self.__class__) | |
# option_prefix = prefix(DarwinTrackpadListener, self.__class__) | |
option_prefix = 'darwin_' # FIXME | |
self._options = { | |
key[len(option_prefix):]: value | |
for key, value in kwargs.items() | |
if key.startswith(option_prefix)} | |
self._intercept = self._options.get('intercept', None) | |
# Try to get an NSView so we can call setAcceptsTouchEvents. | |
# Method 1: NSApplication.sharedApplication() | |
self._application = NSApplication.sharedApplication() | |
window = self._application.keyWindow() | |
print('*** key window', window) # .. None. Are there *any* windows? | |
print('*** windows', self._application.windows()) # empty. | |
# ok, it seems like there aren't any windows.. how can we get a view? | |
# Method 2: NSWorkspace -> NSRunningApplication -> ...? | |
running_app = NSWorkspace.sharedWorkspace().frontmostApplication() | |
super(DarwinTrackpadListener, self).__init__(*args, **kwargs) | |
def _handle(self, _proxy, event_type, event, _refcon): | |
# kCGEventScrollWheel (22) | |
# NSEventTypeGesture (29) | |
# NSEventTypeMouseMoved / kCGEventMouseMoved (5) | |
print('** event', event_type, event) | |
try: | |
# This is the location of the mouse pointer at this point, not the | |
# touches on the trackpad. | |
(px, py) = Quartz.CGEventGetLocation(event) | |
# print(' ', px, py) | |
except AttributeError: | |
pass | |
if event_type == 29: | |
ns_event = NSEvent.eventWithCGEvent_(event) | |
# touches = ns_event.touchesMatchingPhase_inView_(Quartz.NSTouchPhaseAny, None) | |
touches = ns_event.allTouches() | |
if touches: | |
print(touches) # empty | |
def main(): | |
def my_callback(event): | |
print('**** my_callback', event) | |
def _darwin_intercept(_, event): | |
"""For MacOS support. | |
The user must grant Accessibility access to Terminal, in the | |
Security & Privacy > Privacy system preferences. | |
""" | |
return event | |
listener = DarwinTrackpadListener( | |
on_event=my_callback, darwin_intercept=_darwin_intercept) | |
listener.start() | |
listener.wait() | |
try: | |
while True: | |
time.sleep(0.0005) | |
except KeyboardInterrupt: | |
listener.stop() | |
listener.join() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment