Skip to main content


Showing posts from June, 2019

How to emulate special Apple keys (Media navigation, power, etc)

Media keys along with some other keys are not handled through the standard apple keyboard event subsystem. They are treated differently, and therefore emulating them from software requires special code (EventTap won't work!). Here's a function that does this: void HIDPostAuxKey(uint32_t key, bool down) { @autoreleasepool { NSEvent* ev = [NSEvent otherEventWithType:NSEventTypeSystemDefined location:NSZeroPoint modifierFlags:(down ? 0xa00 : 0xb00) timestamp:0 windowNumber:0 context:nil subtype:8 data1:(key << 16)| ((down ? 0xa : 0xb) << 8) data2:-1 ]; CGEventPost(kCGHIDEventTap, [ev CGEvent]); } } This Objective-C code is adapted from an SO post that s

Converting a std::vector into an NSArray

An excellent piece of code to do the above is here : //clang++ -std=c++11 -stdlib=libc++ -framework Foundation -o nsarray /* Note: * - libstdc++ has been frozen by Apple at a pre-C++11 version, so you must opt for the newer, BSD-licensed libc++ * - Apple clang 4.0 (based on LLVM 3.1svn) does not default to C++11 yet, so you must explicitly specify this language standard. */ /* @file * @author Jeremy W. Sherman * * Demonstrates three different approaches to converting a std::vector * into an NSArray. */ #import #include #include #include int main(void) { @autoreleasepool { /* initializer list */ std::vector strings = {"a", "b", "c"}; /* uniform initialization */ //std::vector strings{"a", "b", "c"}; /* Exploiting Clang's block->lambda bridging. */ id nsstrings = [NSMutableArray new]; std::for_each(strings.begin(), strings.end(), ^(std::string str) { id nsstr = [N

Set/get volume in OS X

How to get/set master volume in OS X. Many sample codes online use the deprecated API. This uses the currently 'legal' API. #include <CoreAudio/CoreAudio.h> #include <AudioToolbox/AudioToolbox.h> #include <AudioToolbox/AudioServices.h> #include <AppKit/AppKit.h> #include <CoreGraphics/CGEvent.h> static AudioDeviceID getDefaultOutputDeviceID() { AudioDeviceID outputDeviceID = kAudioObjectUnknown; // get output device device OSStatus status = noErr; AudioObjectPropertyAddress propertyAOPA; propertyAOPA.mScope = kAudioObjectPropertyScopeGlobal; propertyAOPA.mElement = kAudioObjectPropertyElementMaster; propertyAOPA.mSelector = kAudioHardwarePropertyDefaultOutputDevice; if (!AudioObjectHasProperty( kAudioObjectSystemObject, &propertyAOPA)) { printf("Cannot find default output device!"); return outputDeviceID; } status = AudioObjectGetPropert