如何允许箭头键操作图形NSDatePicker? [英] How can the arrow keys be allowed to operate for graphical NSDatePicker?

查看:271
本文介绍了如何允许箭头键操作图形NSDatePicker?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很简单,但很重要。

This seems simple, but essential.

将NSDatePicker(样式:图形)放在窗口中,构建&运行,然后尝试使用箭头键移动。唯一的反应是电脑中的叮ing。

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.

我已经在简单的自定义中覆盖了 -keydown NSDatePicker子类并打印theEvent以注意它们具有123到126的对应密钥代码。

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

然后,在每个箭头按钮按下后,我再次重新设置日期选择器的日期,里面 -keydown ,像这样:

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??

推荐答案

我意识到这个问题真的很老,但我刚刚工作对此,所以...
以下方法适用于我。

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];
    }
}

注意这是一个图形日期选择器自定义NSTextFieldCell。自定义textfieldcell通过修改所选日期来处理快捷键(replacementKey),如:

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];
}

这篇关于如何允许箭头键操作图形NSDatePicker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆