unknown
unknown

Reputation: 978

Make keydown NSEvent run once in Swift

NSEvent keydown code is running repeatedly when key is pressed. How do I make sure that the event runs only once then stop listening until key up event occurs?

    override func viewDidLoad() {
        super.viewDidLoad()
        NSEvent.addLocalMonitorForEvents(matching: .keyDown, handler: doSomething(event:))
    }
    
    func doSomething(event: NSEvent) -> NSEvent{
        // 36 is key code for ENTER key 
        if event.keyCode == 36{
            print("Hello World!")
        }
        return event
    }

Upvotes: 3

Views: 548

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236260

You can simply check if the NSEvent is a repeat using isARepeat property:

func doSomething(event: NSEvent) -> NSEvent {
    if event.keyCode == 36, !event.isARepeat {
        print("Hello World!")
    }
    return event
}

If you just want to get rid of the key sound you need to change your return type to optional and return nil:

func doSomething(event: NSEvent) -> NSEvent? {
    guard !event.isARepeat else {
        // this will suppress the key sound if you return nil and not propagate the key down event
        return nil
    }
    if event.keyCode == 36 {
        print("Hello World!")
        // if you want to suppress the key sound when is not a repeat just return nil here as well
        return nil
    }
    
    return event
}

Upvotes: 4

Related Questions