Last active
November 14, 2022 19:36
-
-
Save adamberenzweig/c1c69ca320eab76440e4be5a01ca326b to your computer and use it in GitHub Desktop.
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 time | |
from pybmi.utils import pynput | |
kIOHIDAccessTypeDenied = 1 | |
kIOHIDAccessTypeGranted = 0 | |
kIOHIDAccessTypeUnknown = 2 | |
kIOHIDRequestTypeListenEvent = 1 | |
kIOHIDRequestTypePostEvent = 0 | |
def load_iokit(): | |
try: | |
import objc | |
from Foundation import NSBundle | |
IOKit = NSBundle.bundleWithIdentifier_('com.apple.framework.IOKit') | |
iokit = {} | |
functions = [ | |
("IOHIDRequestAccess", b"BI"), | |
("IOHIDCheckAccess", b"II"), | |
] | |
objc.loadBundleFunctions(IOKit, iokit, functions) | |
return iokit | |
except Exception as e: | |
logger.error(e) | |
def is_keyboard_verified(): | |
ioset = load_iokit() | |
status = ioset['IOHIDCheckAccess'](kIOHIDRequestTypeListenEvent) | |
print("Keyboard verification status: {}".format(status)) | |
return status == kIOHIDAccessTypeGranted | |
def main(): | |
def on_move(x, y): | |
print('**** on_move', x, y) | |
def _darwin_intercept(event_type, event): | |
"""For MacOS support. | |
The user must grant Accessibility access to Terminal, in the | |
Security & Privacy > Privacy system preferences. | |
""" | |
print('intercept') | |
# returning None should delete the event, to suppress OS-level event handling. | |
# https://developer.apple.com/documentation/coregraphics/cgeventtapcallback?language=objc | |
# https://pynput.readthedocs.io/en/latest/faq.html#macos | |
# FIXME but it doesn't! even with suppress=True | |
return None | |
#return event | |
_, mouse = pynput.maybe_import_pynput() | |
is_keyboard_verified() | |
with mouse.Listener(suppress=True, on_move=on_move, darwin_intercept=_darwin_intercept) as listener: | |
print('is trusted:', listener.IS_TRUSTED) # True | |
listener.join() | |
# listener = mouse.Listener(on_move=on_move, darwin_intercept=_darwin_intercept, suppress=True) | |
# listener.start() | |
# listener.wait() | |
# print('is trusted:', listener.IS_TRUSTED) # True | |
# 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