下面是我的代码。但是我怎么知道这个字符是否来自字母表呢?(任何字母,不只是a-z)
- (void)keyDown:(NSEvent *)theEvent
{
NSString * const character = [theEvent charactersIgnoringModifiers];
}
您可以使用NSCharacterSet
:
- (void)keyDown:(NSEvent *)theEvent
{
NSString * const character = [theEvent charactersIgnoringModifiers];
if ([character length] > 0)
{
unichar c = [character characterAtIndex:0];
NSCharacterSet *letters = [NSCharacterSet letterCharacterSet];
if ([letters characterIsMember:c])
{
NSLog(@"that's a letter!");
}
}
}
如果你经常调用这段代码,可能需要将letters
存储为实例变量