IosViewController not reacting when adding key command

@interface CallbackClass : NSObject

  • (void) doSomething:(UIKeyCommand*)keycmd;
  • (UIKeyCommand*) returnKeyCommand;

@end

@implementation CallbackClass

  • (void) doSomething:(UIKeyCommand*)keycmd { NSLog(@"KEY CLICKED");

} -(UIKeyCommand*) returnKeyCommand { return [UIKeyCommand keyCommandWithInput:@"a" modifierFlags:0 action:@selector(doSomething:)]; } @end

void CppClass::bindKeyCommand() { CallbackClass* callbackClass = [[CallbackClass alloc] init];

[[UIApplication sharedApplication].keyWindow.rootViewController]
               addKeyCommand:[callbackClass returnKeyCommand]];

}

This is objective-c code that is injected into QT application with C++ and Cmake. Responder chain after executing this code:

<QUIView> <QIOSDesktopManagerView> <QIOSViewController> key commands: <UIKeyCommand: 0x12....> -> Title: Action: doSomething: Input: @"a" ...

Even if this key command is present is in the responder chain i dont have any reaction on click on magic keyboard. When i do this in the pure objective-c but not with with QT, C++, Cmake project.

I Detect key input but only when i add my custom view controller do the subview of QIOSViewController, but then i cany click anything else on my application then.

I want to be able to detect key input or somehow inject a responder into responder chain and still being able to click things on my application.

Answered by Frameworks Engineer in 796171022

There needs to be a UIResponder (e.g. a UIView, UIViewController, etc.) in the responder chain that responds to the -doSomething: action. So it's not enough to have your key command in the responder chain. From what I'm seeing, your CallbackClass can provide a key command, and while it does respond to the -doSomething: action, it is not in the responder chain (nor can it be in the chain, as it is not a UIResponder).

I would recommend having one of your existing views/view controllers implement the -doSomething: action. Alternatively, if you want this key command to be performable at any level of the app, you can have UIApplication or your AppDelegate respond to the action.

Accepted Answer

There needs to be a UIResponder (e.g. a UIView, UIViewController, etc.) in the responder chain that responds to the -doSomething: action. So it's not enough to have your key command in the responder chain. From what I'm seeing, your CallbackClass can provide a key command, and while it does respond to the -doSomething: action, it is not in the responder chain (nor can it be in the chain, as it is not a UIResponder).

I would recommend having one of your existing views/view controllers implement the -doSomething: action. Alternatively, if you want this key command to be performable at any level of the app, you can have UIApplication or your AppDelegate respond to the action.

IosViewController not reacting when adding key command
 
 
Q