运行macruby时没有输入到NSTextField
我有一个简单的macruby应用程序,我想通过 NSTextField 获取一些用户输入,但是当我使用“macruby test.rb”运行应用程序时,聚焦文本字段上的所有输入都会进入bash。我需要设置什么才能将输入输入到文本字段中吗?
# 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要添加以下代码(在 application.run 之前的任何位置):
我自己对 MacRuby 很陌生,但这似乎是必要的,因为非捆绑应用程序的默认激活策略是“NSApplicationActivationPolicyProhibited”。
请参阅 Apple 文档 了解更多详细信息。
您可能还会发现使用以下命令激活应用程序很有用:
如果您使用第二行代码,则值得阅读 问题 5032203。如您所知:窃取焦点被认为是不礼貌的,因此如果您曾捆绑应用程序进行分发,则需要删除此行为。
You need to add the following code (anywhere before application.run):
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:
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 邮件列表上的此主题。
为了回答这个问题,下面是让您的应用程序在从命令行启动时获得焦点所需的代码:
See this thread on the MacRuby mailing list.
To answer the question, here is the code needed to let your app have focus when started from the command line: