运行macruby时没有输入NSTextField [英] No input to NSTextField when running macruby
问题描述
我有一个简单的macruby应用程序,并且我想通过NSTextField获得一些用户输入,但是当我使用'''macruby test.rb'''运行该应用程序时,焦点文本字段上的所有输入都进入了重击.我需要设置一些内容,以将输入内容输入文本字段吗?
i have a simple macruby application and i want to get some user-input via a NSTextField, but when i run the application with '''macruby test.rb''' all the input on the focused text-field goes into the bash. is there something that i need to set, to get the input into the text-field?
# test.rb
framework 'AppKit'
application = NSApplication.sharedApplication
frame = [0.0, 0.0, 300, 200]
window = NSWindow.alloc.initWithContentRect(frame,
styleMask: NSTitledWindowMask | NSClosableWindowMask,
backing: NSBackingStoreBuffered,
defer: false)
content_view = NSView.alloc.initWithFrame(frame)
window.contentView = content_view
tf = NSTextField.alloc.initWithFrame([125, 30, 120, 20])
window.contentView.addSubview(tf)
window.display
window.makeKeyAndOrderFront(nil)
application.run
推荐答案
您需要添加以下代码(在application.run之前的任何位置):
You need to add the following code (anywhere before application.run):
application.activationPolicy = NSApplicationActivationPolicyRegular
我本人对MacRuby还是很陌生,但这似乎是必要的,因为非捆绑应用程序的默认激活策略是"NSApplicationActivationPolicyProhibited".
I'm quite new to MacRuby myself, but this seems to be necessary because the default activation policy for unbundled applications is "NSApplicationActivationPolicyProhibited".
See Apple's documentation for more detail.
您可能还会发现使用以下方法激活应用程序很有用:
You may also find it useful to activate your application with the following:
application.activateIgnoringOtherApps(true)
如果您使用第二行代码,则值得阅读问题5032203 .如您所知:窃取焦点被认为是不礼貌的,因此,如果您捆绑了要分发的应用程序,则希望将其删除.
If you use this second line of code it's worth reading through question 5032203. As you know: it is considered impolite to steal focus, so you'd want to remove this if you ever bundled your app for distribution.
这篇关于运行macruby时没有输入NSTextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!