Search code examples
keyboard-shortcutsnspopover

Cut/copy/paste keyboard shortcuts not working in NSPopover


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


Solution

  • 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!