Objective-C on macOS - get keyDown: for Shift+Tab in NSView

ⅰ亾dé卋堺 提交于 2019-12-24 07:18:49

问题


I have an application that processes keystrokes via keyDown.

This works well so far, with the exception of two problems (I'll post/ask the other later).

The problem here is the Shift+Tab combo. I would like to process it myself, but as explained in the Mac OS keyboard event doc, it is used to move the focus forward.

I tried capturing it under

-(BOOL)performKeyEquivalent:(NSEvent *)nsevent
{

}

where I do get an event for it, returning TRUE or FALSE but it still does not appear in keyDown:

I also know about selectNextKeyView: but ideally I am looking for a way to get the system to pass the combination to keyDown: for normal handling (incidentally keyUp: is called correctly for it).

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

回答1:


Answering my own question:

I solved it by intercepting selectNextKeyView and selectPreviousKeyView and forwarding the [NSApp currentEvent] to my keyboard handler (the one that is called on keyDown: for all other keys)

-(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];
}


来源:https://stackoverflow.com/questions/40088407/objective-c-on-macos-get-keydown-for-shifttab-in-nsview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!