【问题标题】:Objective-C on macOS - get keyDown: for Shift+Tab in NSViewmacOS 上的 Objective-C - 获取 keyDown:用于 NSView 中的 Shift+Tab
【发布时间】:2017-02-26 13:21:24
【问题描述】:

我有一个通过 keyDown 处理击键的应用程序。

到目前为止效果很好,除了两个问题(我稍后会发布/询问另一个)。

这里的问题是 Shift+Tab 组合。我想自己处理它,但正如Mac OS keyboard event doc 中所述,它用于将焦点向前移动。

我尝试在

下捕获它
-(BOOL)performKeyEquivalent:(NSEvent *)nsevent
{

}

我确实得到了一个事件,返回 TRUE 或 FALSE,但它仍然没有出现在 keyDown 中:

我也知道 selectNextKeyView: 但理想情况下,我正在寻找一种方法让系统将组合传递给 keyDown: 以进行正常处理(顺便说一下 keyUp: 被正确调用)。

-(void)selectNextKeyView:(id)sender
{
    // shift+tabbed
}

【问题讨论】:

    标签: objective-c macos cocoa keyboard


    【解决方案1】:

    回答我自己的问题:

    我通过拦截 selectNextKeyView 和 selectPreviousKeyView 并将 [NSApp currentEvent] 转发到我的键盘处理程序(在 keyDown 上调用的处理程序:对于所有其他键)来解决它

    -(void)selectNextKeyView:(id)sender
    {
        // this is part of the keyboard handling.  Cocoa swallows ctrl+Tab and
        // calls sselectNextKeyView, so we create a matchin key event for the occasion
        NSEvent *nsevent= [NSApp currentEvent];
        [self handleKeyEvent:nsevent isRaw:FALSE isUp:FALSE];
    }
    
    -(void)selectPreviousKeyView:(id)sender
    {
        // this is part of the keyboard handling.  Cocoa swallows ctrl+shift+Tab and
        // calls selectPreviousKeyView, so we create a matchin key event for the occasion
        NSEvent *nsevent= [NSApp currentEvent];
        [self handleKeyEvent:nsevent isRaw:FALSE isUp:FALSE];
    }
    

    【讨论】:

    • 这并不理想,因为这些方法可能会因为其他原因而被调用,而不仅仅是 Shift+Tab。
    • 另外,您应该考虑使用其他组合键,因为用户将习惯使用 Tab 和 Shift+Tab 来导航控件,并且会惊讶于它不适用于您的应用程序。跨度>
    猜你喜欢
    • 2017-02-26
    • 1970-01-01
    • 2012-06-24
    • 2015-06-05
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    相关资源
    最近更新 更多