No capturing keyboard without title

I am trying to create an animation using spriteKit and this can be controlled using the keyboard (arrow keys to speed up, slow down, rewind the animation).

Also I need this app to have a completely transparent background, I succeeded:

scene?.backgroundColor = NSColor.clearColor()

      

and:

self.window.opaque = false

      

Everything is working fine so far and I can control my animation. But as soon as I try to remove the title bar in Interface Builder by unchecking the checkbox for my window in the right pane, the keyboard capture stops working.

keyDown:

is no longer called and I get a "dong" sound specific to your Mac telling you that keyboard input is not an option. Although I still have my application name in the menu bar.

Is there a way to get the keyboard input option when the title is off?

+3


source to share


2 answers


By default, NSWindow

instances are returned false

from canBecomeKeyWindow

if there is no title bar in the window. The following quote is from the relevant section in the reference NSWindow

.

Attempts to make a window the key window are abandoned if this method returns false. The implementation NSWindow

returns true if the window has a title bar or resizable pane, or false otherwise.



So, to get the behavior you want after subclassing NSWindow

and return true

from canBecomeKeyWindow

.

+4


source


As pointed out by Paul Pattersion (the accepted answer), the trick was to subclass NSWindow to return true for canBecomeKeyWindow. For someone wondering how to do it, here is the code:



import Cocoa

class CustomWindow: NSWindow {
    override var canBecomeKeyWindow: Bool {
        get { return true }
    }
}

      

+1


source







All Articles