Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

datepicker - How can the arrow keys be allowed to operate for graphical NSDatePicker?

This seems simple, but essential.

Place an NSDatePicker (style: graphical) into a window, build & run, then attempt to use arrow keys to move around. The only response is a 'ding' from the computer.

I've overridden -keydown in a simple custom NSDatePicker subclass and printed theEvent to notice they have corresponding keyCodes of 123 through 126.

Then, I stumbled upon resetting the date picker's date after each arrow button press, inside -keydown, like so:

- (void)keyDown:(NSEvent *)theEvent
    switch ([[theEvent valueForKey:@"keyCode"] integerValue]) {
        case 126:
            // Take away one week from the date
            self.dateValue = [NSDate dateWithTimeInterval:-604800 sinceDate:self.dateValue];
            break;
        .... similar for 125 - 123 ...
    }
}

Implementing this method has the side effect of taking away the 'tab' key for stepping through objects.

This seems like a big work-around. Is there another way already included in the date picker??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I realize this question is really old, but I was just working on this, so... The below approach works for me.

- (void)keyDown:(NSEvent *)theEvent
{
    NSString* replacementKey = nil;

    switch ([theEvent keyCode])
    {
        case 123:    // Left arrow
            replacementKey = @"-";
            break;
        case 124:    // Right arrow
            replacementKey = @"+";
            break;
        case 125:    // Down arrow
            replacementKey = @"]";
            break;
        case 126:    // Up arrow
            replacementKey = @"[";
            break;
        case 53: // esc
        case 36: // return  -- don't assign replacement key and these will dismiss date picker
            break;

        default:
            replacementKey = [theEvent charactersIgnoringModifiers];
            break;
    }

    if (replacementKey) // 'letter' based shortcut keys
    {
        [self.textFieldCell dateShortcut:replacementKey];
        [self updateGraphicalCalendar];
    }
    else
    {       
        [self.textFieldCell hidegraphicCalendar];
    }
}

Note this is for a graphical date picker associated with a custom NSTextFieldCell. The custom textfieldcell handles the shortcut keys (the "replacementKey") by modifying the selected date, like:

if (NSOrderedSame == [shortcut caseInsensitiveCompare:@"+"] || NSOrderedSame == [shortcut caseInsensitiveCompare:@"="]) // '=' is unshifted '+'
{
    emptyComponents.day = 1;
    currentDateValue = [calendar dateByAddingComponents:emptyComponents toDate:currentDateValue options:0];
}

else if (NSOrderedSame == [shortcut caseInsensitiveCompare:@"-"])
{
    emptyComponents.day = -1;
    currentDateValue = [calendar dateByAddingComponents:emptyComponents toDate:currentDateValue options:0];
}

else if (NSOrderedSame == [shortcut caseInsensitiveCompare:@"["]) // same day in previous week
{
    emptyComponents.day = -7;
    currentDateValue = [calendar dateByAddingComponents:emptyComponents toDate:currentDateValue options:0];
}

else if (NSOrderedSame == [shortcut caseInsensitiveCompare:@"]"]) // same day in next week
{
    emptyComponents.day = 7;
    currentDateValue = [calendar dateByAddingComponents:emptyComponents toDate:currentDateValue options:0];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...