Loading...

How to detect Escape key pressed in macOS apps?

question macOS swift
Ram Patra Published on March 14, 2021

Like Delete key, detection of Escape key press is also slightly different than detecting general key presses.

if Int(event.keyCode) == kVK_Escape {
  print("Escape key pressed!")
}

Below is the full sample code:

NSEvent.addLocalMonitorForEvents(matching: .keyDown) {
  if self.keyDown(with: $0) {
    return nil // needed to get rid of purr sound
  } else {
    return $0
  }
}

private func keyDown(with event: NSEvent) -> Bool {
  if Int(event.keyCode) == kVK_Escape {
    print("Escape key pressed!")
    return true
  } else {
    return false
  }
}
Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

ToDoBar

Your to-dos on your menu bar.

Ram Patra Published on March 14, 2021
Image placeholder

Keep reading

If this article was helpful, others might be too

question macOS swift August 6, 2020 How to open an app's window on top of all others in Swift?

You can open your app’s window on top of all other open application windows with the below code:

question macOS swift August 6, 2020 How to keep an app's window always on top of others in Swift?

Before launching the window, just use the appropriate window level, and you’re done.

October 17, 2024 Show your screen and your face at the same time on macOS

In today’s world of remote work and digital content creation, engaging your audience has never been more important. Whether you’re giving a presentation, conducting a tutorial, or recording a demo, adding a personal touch can make all the difference. That’s where FaceScreen comes in. This innovative macOS app allows you to open up your camera view in Picture-in-Picture (PiP) mode, enabling you to show both your screen and your face simultaneously.