• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Swift 3 macOS read keyboard
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Swift 3 macOS read keyboard


  • Subject: Re: Swift 3 macOS read keyboard
  • From: "Eric E. Dolecki" <email@hidden>
  • Date: Mon, 10 Oct 2016 14:45:10 +0000

I just discovered

override func keyDown(with event: NSEvent) { }
override func keyUp(with event: NSEvent) {}

NSEvent.addLocalMonitorForEvents(matching: .keyUp) { (aEvent) -> NSEvent? in
            self.keyUp(with: aEvent)
            return aEvent
        }
        NSEvent.addLocalMonitorForEvents(matching: .keyDown) { (aEvent) ->
NSEvent? in
            self.keyDown(with: aEvent)
            return aEvent
        }

So I have it working now without subclassing NSView, etc. It took me a
while to find that bit about NSEvent.addLocalMonitorForEvents...

Eric

On Mon, Oct 10, 2016 at 8:53 AM Eric E. Dolecki <email@hidden> wrote:

> I am delving into macOS development and I'd like to do something simple.
> Give the Application Window "focus" to receive keyboard events. Meaning no
> text fields.
>
> How exactly do I do this? I've been googling and have seen all kinds of
> things but none of them have worked out. What would be the most
> straightforward way of kicking off keyUp and keyDown from within my main
> NSViewController so I can read keys?
>
> Thanks,
> Eric
>
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Swift 3 macOS read keyboard
      • From: Graham Cox <email@hidden>
References: 
 >Swift 3 macOS read keyboard (From: "Eric E. Dolecki" <email@hidden>)

  • Prev by Date: Swift 3 macOS read keyboard
  • Next by Date: State restoration Debugging
  • Previous by thread: Swift 3 macOS read keyboard
  • Next by thread: Re: Swift 3 macOS read keyboard
  • Index(es):
    • Date
    • Thread