Cut/copy/paste keyboard shortcuts not working in NSPopover
Asked Answered
D

1

2

I have a macOS NSPopover based tray app which shows a popover with login fields (username and password).

Problem is that user is unable to copy-paste his email or password into text fields. The popover doesn't seem to allow keyboard shortcuts for some reason.

Did anyone have similar issues?

Relevant example available here: https://github.com/mixtly87/NSPopoverTest

Depilate answered 3/4, 2018 at 19:22 Comment(0)
H
2

This isn't the easiest thing to solve and you need to do a few things to get this to work.

1 ) add a MainMenu to your MainMenu.xib file.

Even though the main menu won't display (because you're only doing a NSStatusBar item), you want that main menu because of the command keys in the Edit menu (i.e. something to intercept the cmd-X, cmd-C & cmd-V's). Those command keys will be sent to your text field or your webview, whatever is the first responder.

More info can be seen here.

2 )

I made your textfield the first responder by adding:

- (void)viewDidAppear
{
    [super viewDidAppear];
    [self.textField becomeFirstResponder];
}

to your ViewController.m file.

3 )

You also need to make the window brought up by the Status Item a key window. In your example app, you did have a commented out canBecomeKeyWindow method. I uncommented it out and always return TRUE.

More info can be seen here.

Hope this helps!

Haakon answered 3/4, 2018 at 20:35 Comment(2)
Thanks Michael, this was really helpful! In my case though, your solution works even without overriding the canBecomeKeyWindow and returning YES.Depilate
Is there anyway to do this when you are not using a Storyboard or InterfaceBuilder for your app?Nidorf

© 2022 - 2024 — McMap. All rights reserved.