-
-
Save fredrikw/4078034 to your computer and use it in GitHub Desktop.
#!/usr/bin/python | |
# CLI program to control the mediakeys on OS X. Used to emulate the mediakey on a keyboard with no such keys. | |
# Easiest used in combination with a launcher/trigger software such as Quicksilver. | |
# Main part taken from http://stackoverflow.com/questions/11045814/emulate-media-key-press-on-mac | |
# Glue to make it into cli program by Fredrik Wallner http://www.wallner.nu/fredrik/ | |
import Quartz | |
import sys | |
# NSEvent.h | |
NSSystemDefined = 14 | |
# hidsystem/ev_keymap.h | |
NX_KEYTYPE_SOUND_UP = 0 | |
NX_KEYTYPE_SOUND_DOWN = 1 | |
NX_KEYTYPE_PLAY = 16 | |
NX_KEYTYPE_NEXT = 17 | |
NX_KEYTYPE_PREVIOUS = 18 | |
NX_KEYTYPE_FAST = 19 | |
NX_KEYTYPE_REWIND = 20 | |
supportedcmds = {'playpause': NX_KEYTYPE_PLAY, 'next': NX_KEYTYPE_NEXT, 'prev': NX_KEYTYPE_PREVIOUS, 'volup': NX_KEYTYPE_SOUND_UP, 'voldown': NX_KEYTYPE_SOUND_DOWN} | |
def HIDPostAuxKey(key): | |
def doKey(down): | |
ev = Quartz.NSEvent.otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_( | |
NSSystemDefined, # type | |
(0,0), # location | |
0xa00 if down else 0xb00, # flags | |
0, # timestamp | |
0, # window | |
0, # ctx | |
8, # subtype | |
(key << 16) | ((0xa if down else 0xb) << 8), # data1 | |
-1 # data2 | |
) | |
cev = ev.CGEvent() | |
Quartz.CGEventPost(0, cev) | |
doKey(True) | |
doKey(False) | |
if __name__ == "__main__": | |
try: | |
command = sys.argv[1] | |
assert(command in supportedcmds) | |
HIDPostAuxKey(supportedcmds[command]) | |
except (IndexError, AssertionError): | |
print "Usage: %s command" % (sys.argv[0],) | |
print "\tSupported commands are %s" % supportedcmds.keys() |
I had tried that before expanding the command and got the same result. Someone on the Alfred Forum converted it to Swift for me 🙂
Hello there, I working on Mac OS Catalina 10.15.7, when I installed Quartz I got warning:
Cannot find reference 'CGEventPost' in '__init__.py | __init__.py'
How to solve them?
As I said above, this script is really dated so I don't remember and I don't use it any longer either. So I'm afraid I cannot help much. Did you really get the error when you installed Quartz?
I already installed quartz.However your code is working, despite the error.
And I want to ask you, where did you find Quartz documentation? I have seen the official Quartz documentation but have not found an explanation for it methods this framework.
I just copied the main thing from the StackOverflow answer that is linked in the top comment of the script. My only contribution was adding the CLI.
I've updated the script for python 3 and worked out the correct egg to install:
https://gist.github.com/aleksandersumowski/a296a910fd3c6722744a2c3b0a1b5bf5
the script assumes python 3 installed via homebrew on a M* computer
So then probably change the first line to
#!/usr/local/bin/python
That should probably work looking at your result from
/usr/local/bin/python -m pip install pyobjc-framework-Quartz
Python 2.7 should be avoided by now