Forum Xamarin.iOS


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

KeyDown cannot fired when AddKeyCommand to UIViewController

anranstlanranstl CNMember
edited December 2017 in Xamarin.iOS


I'm using UIKeyCommand to add detect for hardware keyboard. And I got a UIView which added to KeyWindow like following:
UIApplication.SharedApplication.KeyWindow.AddSubview(MyView); UIApplication.SharedApplication.KeyWindow.BringSubviewToFront(MyView);

Then I add KeyCommand in MyView like this:
ObjCRuntime.Selector selector = new ObjCRuntime.Selector("OnKeyDown:");
UIKeyCommand CommandUp = UIKeyCommand.Create(KeyboardEx.Up, 0, selector, (NSString)"Up");
UIViewController controller = GetCurrentViewController();
if (controller != null)

And then my export:
private void OnKeyDown(UIKit.UIKeyCommand cmd)


And the code used to get current controller:
internal UIViewController GetCurrentViewController()
var window = UIApplication.SharedApplication.KeyWindow;
var vc = window.RootViewController;
while (vc.PresentedViewController != null)
vc = vc.PresentedViewController;
return vc;

Question is, OnKeyDown never fired, I use these same code on a normal view(which not added to a keywindow), everything works fine, I'm not sure what's going on. Can anybody help?

Sign In or Register to comment.