The "Funk" Sound When Hitting Escape Key in App

the funk sound when hitting escape key in app

Add a new file, cocoa class to your project, click next, create a CustomView class, subclass of NSView (language Swift). Override the method performKeyEquivalent and add return event.keyCode == 53. Then select your window view in your IB, go to the identity inspector and change the custom class to CustomView:

import Cocoa

class CustomView: NSView {
override func performKeyEquivalent(with event: NSEvent) -> Bool {
return event.keyCode == 53
}
}

How to handle escape key press event?

Create a subclass of NSWindow and implement keyDown event:

import Cocoa
import Carbon.HIToolbox

class CustomWindow: NSWindow {

override func keyDown(with event: NSEvent) {
switch Int(event.keyCode) {
case kVK_Escape:
print("Esc pressed")
default:
break
}
super.keyDown(with: event)
}

}

This line:

import Carbon.HIToolbox

Lets you use handy constants for keys, such as kVK_Escape.

Set this class as your main window class in the Interface Builder and you're all set:

Sample Image

Sample Image

P.S. To do the same form NSViewController, in viewDidLoad do:

NSEvent.addLocalMonitorForEvents(matching: .keyDown) {
self.keyDown(with: $0)
return $0
}

P.P.S. To mute "bang" sound, don't call super upon Escape key press - move super call to default:

default:
super.keyDown(with: event)

EDIT:

If you don't want any sound on Escape key press, then the following approach should be used:

Make an NSView subclass and set it to main view of the view controller:

import Cocoa
import Carbon.HIToolbox

class CustomView: NSView {
override func performKeyEquivalent(with event: NSEvent) -> Bool {
switch Int(event.keyCode) {
case kVK_Escape:
print("Esc pressed")
return true
default:
return super.performKeyEquivalent(with: event)
}
}
}

Support closing of a NSWindow presented as sheet via escape key

You have to add a regular button and position it at the top left corner of your window. Set your button width and alpha to 0.

Sample Image Sample Image

Click at the key equivalent field and press ESC. It will show the escape key icon

Sample Image

Then create your IBAction for the hidden button as follow:

@IBAction func closeWindowAction(sender: AnyObject) {

window.close()
}

Make sure you have your window outlet connected to the proper window:

@IBOutlet weak var window: NSWindow!

Swift - Capture keydown from NSViewController

Xcode 8.2.1 • Swift 3.0.2

import Cocoa

class ViewController: NSViewController {

@IBOutlet var textField: NSTextField!

override func viewDidLoad() {
super.viewDidLoad()
NSEvent.addLocalMonitorForEvents(matching: .flagsChanged) {
self.flagsChanged(with: $0)
return $0
}
NSEvent.addLocalMonitorForEvents(matching: .keyDown) {
self.keyDown(with: $0)
return $0
}
}
override func keyDown(with event: NSEvent) {
switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) {
case [.command] where event.characters == "l",
[.command, .shift] where event.characters == "l":
print("command-l or command-shift-l")
default:
break
}
textField.stringValue = "key = " + (event.charactersIgnoringModifiers
?? "")
textField.stringValue += "\ncharacter = " + (event.characters ?? "")
}
override func flagsChanged(with event: NSEvent) {
switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) {
case [.shift]:
print("shift key is pressed")
case [.control]:
print("control key is pressed")
case [.option] :
print("option key is pressed")
case [.command]:
print("Command key is pressed")
case [.control, .shift]:
print("control-shift keys are pressed")
case [.option, .shift]:
print("option-shift keys are pressed")
case [.command, .shift]:
print("command-shift keys are pressed")
case [.control, .option]:
print("control-option keys are pressed")
case [.control, .command]:
print("control-command keys are pressed")
case [.option, .command]:
print("option-command keys are pressed")
case [.shift, .control, .option]:
print("shift-control-option keys are pressed")
case [.shift, .control, .command]:
print("shift-control-command keys are pressed")
case [.control, .option, .command]:
print("control-option-command keys are pressed")
case [.shift, .command, .option]:
print("shift-command-option keys are pressed")
case [.shift, .control, .option, .command]:
print("shift-control-option-command keys are pressed")
default:
print("no modifier keys are pressed")
}
}
}

To get rid of the purr sound when pressing the character keys you need to subclass your view, override the method performKeyEquivalent and return true.

import Cocoa

class View: NSView {
override func performKeyEquivalent(with event: NSEvent) -> Bool {
return true
}
}

Sample Project



Related Topics



Leave a reply



Submit