NSDatePicker action method not working

Scenario:

The user enters a date in NSDatePicker in their text form without a step (in OS X), and when they click on return on the keyboard, I want the message to be sent to the controller.

In NSTextField I would simply pick up an action in Interface Builder or install it from the code, and when the user enters the input field, a message about the action will be sent to the target.

The date picker allows you to set an action message and a goal, but I cannot start the action. When I press enter in the date picker, the action message is not called.

Am I doing something wrong or is there a workaround that I should use? I would not negatively subclass any of the classes involved, if that is what is required.

+4
source share
1 answer

NSDatePicker will not process or forward key events triggered by the Return or Enter key.

The solution is to subclass NSDatePicker to get the desired behavior in keyDown:

 #import "datePickerClass.h" @implementation datePickerClass - (void)keyDown:(NSEvent *)theEvent{ unsigned short n = [theEvent keyCode]; if (n == 36 || n == 76) { NSLog(@"Return key or Enter key"); // do your action // } else { [super keyDown:theEvent];// normal behavior } } @end 

What is it.

Edit: you can also use NSCarriageReturnCharacter and NSEnterCharacter

 NSString* const s = [theEvent charactersIgnoringModifiers]; unichar const key = [s characterAtIndex:0]; if (key == NSCarriageReturnCharacter || key == NSEnterCharacter) { 
+5
source

Source: https://habr.com/ru/post/1412425/


All Articles