运行macruby时没有输入到NSTextField

发布于 2024-11-28 08:47:40 字数 783 浏览 0 评论 0原文

我有一个简单的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅笑轻吟梦一曲 2024-12-05 08:47:40

您需要添加以下代码(在 application.run 之前的任何位置):

application.activationPolicy = NSApplicationActivationPolicyRegular

我自己对 MacRuby 很陌生,但这似乎是必要的,因为非捆绑应用程序的默认激活策略是“NSApplicationActivationPolicyProhibited”。

请参阅 Apple 文档 了解更多详细信息。

您可能还会发现使用以下命令激活应用程序很有用:

application.activateIgnoringOtherApps(true) 

如果您使用第二行代码,则值得阅读 问题 5032203。如您所知:窃取焦点被认为是不礼貌的,因此如果您曾捆绑应用程序进行分发,则需要删除此行为。

You need to add the following code (anywhere before application.run):

application.activationPolicy = NSApplicationActivationPolicyRegular

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) 

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.

守护在此方 2024-12-05 08:47:40

请参阅 MacRuby 邮件列表上的此主题

为了回答这个问题,下面是让您的应用程序在从命令行启动时获得焦点所需的代码:

NSApplication.sharedApplication.activationPolicy = NSApplicationActivationPolicyRegular
NSApplication.sharedApplication.activateIgnoringOtherApps(true)

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:

NSApplication.sharedApplication.activationPolicy = NSApplicationActivationPolicyRegular
NSApplication.sharedApplication.activateIgnoringOtherApps(true)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文