开发者

Making my NSSecureTextField trigger an NSButton

开发者 https://www.devze.com 2023-02-16 03:08 出处:网络
I have an NSSecu开发者_运维百科reTextField. How can I make it so that when the \'Enter\' button is pressed on the keyboard, the \'login\' button is triggered? Thanks!It\'s probably a better idea to ju

I have an NSSecu开发者_运维百科reTextField. How can I make it so that when the 'Enter' button is pressed on the keyboard, the 'login' button is triggered? Thanks!


It's probably a better idea to just set the keyboard equivalent of the Login button itself to \r(return). To do that, select the button in Interface Builder, open the attributes inspector, and click in the Key Equiv.popup button, then press the return key so that it looks like the image below:

Making my NSSecureTextField trigger an NSButton

This will give the button the blue/graphite color making it the "default" button. Whenever the return or enter key is pressed, this button will be "clicked" automatically.


You can also try this function. Set the delegate of the NSSecureTextField to this function. It will execute when you hit enter in the NSSecureTextField.

- (IBAction)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
{

    if (commandSelector == @selector(insertNewline:))
    {

        //Do login stuff
    }
    else if(commandSelector == @selector(deleteBackward:)){
        NSUInteger length = [[secureText stringValue] length];

        if(length != 0) 
        {
            [secureText setStringValue:[[secureText stringValue] substringToIndex: length-1]];
        }
    }

}

Found example here

EDIT: Added a deleteBackward case to handle backspace since this function ignores backspace.

0

精彩评论

暂无评论...
验证码 换一张
取 消