Cocos2d - Mac:检查键盘事件?

Vol*_*ort 6 keyboard macos events objective-c cocos2d-iphone

我正在使用Cocos2d,这是一个用于创建iPhone游戏的框架.最近他们发布了Mac支持,我正在为Mac制作一个简单的游戏.

但是,我对如何检查Mac键盘上何时按下X键一无所知.

Objective-C的

Dav*_*ins 9

你应该做:

self.isKeyboardEnabled = YES;
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用接收按键的消息:

-(void) ccKeyUp:(NSEvent*)event;
-(void) ccKeyDown:(NSEvent*)event;
Run Code Online (Sandbox Code Playgroud)

这应该做到这一点.

有关更多信息,请参阅此处

http://www.cocos2d-iphone.org/forum/topic/11725


小智 5

这是一个例子:

-(id) init
{
    if( (self=[super init])) 
    {
        .
        .
        .

        self.isKeyboardEnabled = YES ;

        .
        .
        .
    }
}

- (void) ccKeyDown : (NSEvent*) KeyDownEvent
{
    NSString *str_1 = [KeyDownEvent characters];
    unichar ch = [str_1 characterAtIndex:0];

    if ( ch == 97)                  // if 'a' Button Pressed Down
    {
        if ( canMoveRight == NO)
        {
            canMoveLeft = YES ;
        }

    }
    else if ( ch == 100 )           // if 'd' Button Pressed Down
    {
        if ( canMoveLeft == NO)
        {
            canMoveRight = YES ;
        }
    }
}

- (void) ccKeyUp : (NSEvent*) KeyDownEvent
{

    NSString *str_1 = [KeyDownEvent characters];
    unichar ch = [str_1 characterAtIndex:0];

    if ( ch == 97)                  // if 'a' Button Released
    {
        canMoveLeft = NO ;
    }
    else if ( ch == 100 )           // if 'd' Button Released
    {
        canMoveRight = NO ;
    }
}
Run Code Online (Sandbox Code Playgroud)

PS:字符代码基于其ASCII码.