0

1.Есть поле из символов

. . . . .
. @. . 
. . . . .
  1. По нажатию на клавишу клавиатуры на Mac нужно двигать символ @
  2. Всё происходит в консоли Xcode

Как реагировать на события клавиатуры?

2
  • «Всё происходит в консоли Xcode» — эээ, программа будет поставляться клиенту вместе с XCode?
    – VladD
    9 апр 2016 в 15:54
  • Это никуда не будет поставляться! Это просто задача!
    – Artem
    9 апр 2016 в 17:57

1 ответ 1

2

Любой класс, являющийся наследником NSResponder отвечает на keyDown или keyUp

- (void)keyDown:(NSEvent *)theEvent {

NSNotificationCenter *notificationCenter =
[NSNotificationCenter defaultCenter];

unsigned short keyCode = [theEvent keyCode];
if([theEvent modifierFlags] & NSCommandKeyMask && keyCode == 36 /*Cmd+Enter*/)
{
    [notificationCenter postNotificationName:@"cmdEnterKeyPressedNotification"
                                      object:nil];
    return;
}
if(keyCode == 36 /*Enter*/)
{
    [notificationCenter postNotificationName:@"enterKeyPressedNotification"
                                      object:nil];
    return;
}
if(keyCode == 49 /*Space*/)
{
    [notificationCenter postNotificationName:@"spaceKeyPressedNotification"
                                      object:nil];
    return;
}
[super keyDown:theEvent];

}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.