Execute an Action when the Enter-Key is pressed in a NSTextField?

后端 未结 8 1934
野的像风
野的像风 2020-12-02 06:23

I have a small problem right now. I want to execute a method when the Enter key is pressed in a NSTextField. The user should be able to enter his data and a calculation meth

相关标签:
8条回答
  • 2020-12-02 06:47

    You have to do only this

    For some keys (Enter, Delete, Backspace, etc)

    self.textfield.delegate = self;
    

    and then implement this method

    - (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
    {
        NSLog(@"Selector method is (%@)", NSStringFromSelector( commandSelector ) );
        if (commandSelector == @selector(insertNewline:)) {
            //Do something against ENTER key
    
        } else if (commandSelector == @selector(deleteForward:)) {
            //Do something against DELETE key
    
        } else if (commandSelector == @selector(deleteBackward:)) {
            //Do something against BACKSPACE key
    
        } else if (commandSelector == @selector(insertTab:)) {
            //Do something against TAB key
    
        } else if (commandSelector == @selector(cancelOperation:)) {
            //Do something against Escape key
        }
        // return YES if the action was handled; otherwise NO
    } 
    
    0 讨论(0)
  • 2020-12-02 06:52

    NSTextFieldDelegate's – control:textView:doCommandBySelector: is your friend.

    Look for the insertNewline: command.

    0 讨论(0)
提交回复
热议问题