Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy doesn't keyDown get called if I don't use XCode and IB?
    text
    copied!<p>When I subclass a widget and define a keyDown method, and then in InterfaceBuilder assign the class of a widget to be the class I defined with the keyDown method, it seems to work fine.</p> <p>However, if I do it complete programmatically as illustrated in the simple script below, the keyDown method never gets called. Why is this?</p> <p>Is there something being set up by XCode that is causing the app to listen for events that gets done behind the scenes? What is it, and how do I do it here? Please point me in the right direction. </p> <p>I'm gonna go read through the apple docs again and try to figure out if there is an event handler registration going on that XCode is setting for me. However, if you know what it is, please let me know.</p> <p>Thanks!</p> <pre><code>framework 'Cocoa' class MyView &lt; NSView def acceptsFirstResponder true end def keyDown(event) puts "key down MyView" end end class MyWindow &lt; NSWindow def acceptsFirstResponder true end def keyDown(event) puts "key down in MyWin" end end class MyApp &lt; NSApplication def acceptsFirstResponder true end def keyDown(event) puts "key down in MyApp" end end application = MyApp.sharedApplication # create the window frame = [0.0, 0.0, 300, 200] mask = NSTitledWindowMask | NSClosableWindowMask window = MyWindow.alloc.initWithContentRect(frame, styleMask:mask, backing:NSBackingStoreBuffered, defer:false) # assign a content view instance content_view = MyView.alloc.initWithFrame(frame) window.contentView = content_view window.makeFirstResponder(window) # show the window window.display window.makeKeyAndOrderFront(nil) application.run </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload