swiftnsviewkeydown

KeyDown is not working, Swift


I have override keyDown method in class NSView, but I don't get any action.

this is how my code looks like:

class Rotund: NSView {

var numar: Int = 0 {
    didSet {
        self.needsDisplay = true
    }
}

override func drawRect(dirtyRect: NSRect) {
    super.drawRect(dirtyRect)
        drawCanvas1(number: CGFloat(numar), x: 0)
}
func drawCanvas1(#number: CGFloat, x: CGFloat) {
    //// Color Declarations
    let color = NSColor(calibratedRed: 0.98, green: 0.008, blue: 0.008, alpha: 1)

    //// Oval Drawing
    let ovalPath = NSBezierPath(ovalInRect: NSMakeRect(x, 34, number, number))
    color.setFill()
    ovalPath.fill()
}    
override func keyDown(theEvent: NSEvent) {
    if theEvent.keyCode == 124 {
        println("abc")
        } else {
            println("abcd")
    }
}

Can some Help me figure out how should I do?


Solution

  • Just confirming... in your code you seem to have overriden KeyUp. So maybe, you should try changing it to:

    override func keyDown(theEvent: NSEvent) {
    if theEvent.keyCode == 124 {
        println("abc")
    } else {
        println("abcd")
    

    }