Search code examples
macoscocoakeydownnsbutton

How to caputure keyDown event in NSbutton after a mouseClick


I'm having five Image Buttons. If a button was clicked by mouse, then the button went to Onstate. Now if i pressed Space button it will zoom the button's image in a seperate Window. The space button should not work when the button id in OFFstate. now can i trigger the Keyevent? i already referred Quicklook and working on that. any new ideas?


Solution

  • You don't need to trigger the key event, just respond to it as though it were any old method call from the button. After giving it a target and a selector, give it a key equivalent of @" " and respond to the key press accordingly:

    - (void)someAction:(NSButton*)sender event:(NSEvent*)event {
        //...
        if ((sender.state == NSOnState) && (event.keyCode == 0x32)) {
            //Zoom
        }
        //...
    }