Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

KeyDown cannot fired when AddKeyCommand to UIViewController

$
0
0

Hi,

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)
{
controller.AddKeyCommand(CommandUp);
}

And then my export:
[Export("OnKeyDown:")]
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?


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>