ASOC detecting key-presses

Hi MacScripters,
For an application I’m making, I need to know when a user presses any of the arrow keys, and what key it is. Right now I changed my parent class to “NSView” and then changed my parent view’s class to my app delegate in IB so that I could use “keyDown_(theEvent)” to detect when they press a key and which it is. However, it seems that while I’m coding in this function, the application won’t recognize any of the variables I’ve connected to objects in Interface Builder. I’ve tried calling different functions and even calling them on a new thread, but it seems that if the instance originally called the keyDown function, none of my IB connections are being realized and all of my object variables are set to “missing value.” Is there any other way to detect keystrokes? Or even better, is there a way to get the code to recognize my IB connections? Thanks,

~Josh

I’m a little confused by what you’ve done. What do you mean by “changed my parent view’s class to my app delegate in IB”. Can you give a little more detail on what you did in IB? Do you just have one script that’s a subclass of NSView and that is also the appDelegate? And, what do you mean by parent view, the content view of the window?

Ric

Sorry about the confusion. By “Parent View” I meant my main view, or the one I’m using for all of the key presses. And yes, I did make the app delegate a subclass of the NSView. I ended up just making a couple menu items with one key keystrokes instead of trying to detect every key-press and then sort through them for the ones I was looking for. Thanks for the attempted help at least!

I’m not sure that’s the best way to go about it. I think I would leave the app delegate alone, and add another applescript class. Change its parent to NSView, and change the main view’s class to that new script. In that class you just need to override acceptsFirstResponder and keyDown. For instance, this will log key events when the app starts up if I make the main view the initial first responder of the window:

script  MainView
	property parent : class "NSView"
    
    on acceptsFirstResponder()
        return 1
    end
    
    on keyDown_(theEvent)
        log theEvent
    end

end script

Ric