Prevent error funk sound in event monitor OS X
I ended up using MASShortcut as a workaround solution to this issue.
OSX swift 3 - can't disable keyDown. error sound
Update: I've found out that the root cause of the problem was, that a view was the first responder that shouldn't be actually. After setting the responder to nil self.view.window?.makeFirstResponder(nil)
I was able to fix this. I've also used performKeyEquivalent
as this answer suggested.
I know my answer is very late, but maybe it will help you or someone else in the future. I'm not sure if that's the best way to do it but it does work. Simply return nil instead of the event.
NSEvent.addLocalMonitorForEvents(matching: .keyDown) { (aEvent) -> NSEvent? in
self.keyDown(with: aEvent)
return nil
}
override func keyDown(with event: NSEvent) {
super.keyDown(with: event)
}
Apple's documentation of the method says the following for the block
parameter:
The event handler block object. It is passed the event to monitor. You can return the event unmodified, create and return a new NSEvent object, or return nil to stop the dispatching of the event.
The only downside is, that there will be no sound at all. Even if the key event is not handled by you.
Monitoring app switching on OS X
You can add an observer on NSWorkspace.sharedWorkspace().notificationCenter
watching for the NSWorkspaceDidActivateApplicationNotification
key. You point the selector at one of your methods and grab the information from the userInfo
dictionary.
Simple example in AppDelegate:
Swift 2.2
func applicationDidFinishLaunching(notification: NSNotification) {
NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self,
selector: #selector(activated),
name: NSWorkspaceDidActivateApplicationNotification,
object: nil)
}
func activated(notification: NSNotification) {
if let info = notification.userInfo,
app = info[NSWorkspaceApplicationKey],
name = app.localizedName {
print(name)
}
}
Swift 3
func applicationDidFinishLaunching(_ aNotification: Notification) {
NSWorkspace.shared().notificationCenter.addObserver(self,
selector: #selector(activated(_:)),
name: NSNotification.Name.NSWorkspaceDidActivateApplication,
object: nil)
}
func activated(_ notification: NSNotification) {
if let info = notification.userInfo,
let app = info[NSWorkspaceApplicationKey] as? NSRunningApplication,
let name = app.localizedName
{
print(name)
}
}
Change OS X system volume programmatically
You need to get the default audio device first:
#import <CoreAudio/CoreAudio.h>
AudioObjectPropertyAddress getDefaultOutputDevicePropertyAddress = {
kAudioHardwarePropertyDefaultOutputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
AudioDeviceID defaultOutputDeviceID;
UInt32 volumedataSize = sizeof(defaultOutputDeviceID);
OSStatus result = AudioObjectGetPropertyData(kAudioObjectSystemObject,
&getDefaultOutputDevicePropertyAddress,
0, NULL,
&volumedataSize, &defaultOutputDeviceID);
if(kAudioHardwareNoError != result)
{
// ... handle error ...
}
You can then set your volume on channel 1 (left) and channel 2 (right). Note that channel 0 (master) does not seem to be supported (the set command returns 'who?')
AudioObjectPropertyAddress volumePropertyAddress = {
kAudioDevicePropertyVolumeScalar,
kAudioDevicePropertyScopeOutput,
1 /*LEFT_CHANNEL*/
};
Float32 volume;
volumedataSize = sizeof(volume);
result = AudioObjectSetPropertyData(defaultOutputDeviceID,
&volumePropertyAddress,
0, NULL,
sizeof(volume), &volume);
if (result != kAudioHardwareNoError) {
// ... handle error ...
}
Hope this answers your question!
Monitor when app opens file
I was able to mostly solve the problem by using this answer:
let keycode = UInt16(0x1F)
let keymask: NSEvent.ModifierFlags = NSEvent.ModifierFlags.command
//...
let options = NSDictionary(object: kCFBooleanTrue, forKey: kAXTrustedCheckOptionPrompt.takeUnretainedValue() as NSString) as CFDictionary
let trusted = AXIsProcessTrustedWithOptions(options)
if (trusted) {
NSEvent.addGlobalMonitorForEvents(matching: .keyUp, handler: self.handler)
}
Related Topics
Swift Memory Management: Storing Func in Var
Updating a @Published Variable Based on Changes in an Observed Variable
iOS 13, Custom Image, Title and Subtitle in the Presented Uiactivityviewcontroller
How to Check If Core Data Is Empty
Callback Url Not Approved Despite Being Provided Twitter API
Timer Onreceive Not Working Inside Navigationview
Memory Problems When Switching Between Scenes Spritekit
Uiprogressview Progress Update Very Slow Within Alamofire (Async) Call
Implicit Cast Function Receiving Tuple
Using Animoji/Memoji as Profile Photo
Combined Chart (Line- and Bar Chart) Using iOS-Charts
How to Duplicate a Sprite in Sprite Kit and Have Them Behave Differently
Float Is Not Convertible to 'Mirrordisposition' Swift What Is Mirrordisposition