Detect which key is pressed [closed]

走远了吗. 提交于 2019-12-01 13:31:03

问题


How do I tell which key was pressed in a Cocoa Application (I know each key has an associated number)? In my case, I want to log the key to the console.

This is my code:

- (BOOL)acceptsFirstResponder {
return YES;
}


-(void)keyUp:(NSEvent*)event {
NSLog(@"Key %@", event);
}

回答1:


Use the NSEvent methods keyCode, characters or charactersIgnoringModifiers.

- (void)keyUp:(NSEvent *)event {
    NSLog(@"Characters: %@", [event characters]);
    NSLog(@"KeyCode: %hu", [event keyCode]);
}



回答2:


NSEvent has the keyCode method that returns exactly what you're looking for.



来源:https://stackoverflow.com/questions/12770980/detect-which-key-is-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!