如何获取mac键盘上某些按键的状态?

发布于 2024-12-01 02:03:43 字数 1245 浏览 0 评论 0原文

我正在使用 cocos2d for mac (1.0.1)

我有这两种方法(与苹果的方法类似)

//keyboard delegate is defined - these methods are called
- (BOOL)ccKeyDown:(NSEvent *)event {
    unichar key = [[event characters] characterAtIndex:0];
    if (key == NSDownArrowFunctionKey) {
        // Down arrow pressed
    }
    if(key == NSUpArrowFunctionKey) {
        // Up arrow pressed
        return YES;
    }
    if(key == NSLeftArrowFunctionKey) {
        // Left arrow pressed
       something = -1.0f;
        return YES;

    } else if(key == NSRightArrowFunctionKey) {
        // Right arrow pressed
       something = 1.0f;
        return YES;
    } 
    return NO;
}
-(BOOL)ccKeyUp:(NSEvent *)event {
    unichar key = [[event characters] characterAtIndex:0];
    if (key == NSDownArrowFunctionKey) {
        // Down arrow pressed
    }
    if(key == NSUpArrowFunctionKey) {
        // Up arrow pressed
    }
    if(key == NSLeftArrowFunctionKey || key == NSRightArrowFunctionKey) {
        // Left arrow pressed
       something = 0.0f;
    } 

    return YES;
}

我想称之为:

something = 0.0f;

如果左箭头键和右箭头键没有被按下/按住。据我所知,仅当按下某个键然后释放该键时才会调用 ccKeyUp 。如果按下某个键,则返回 ccKeyDown。

如何获取当前键盘状态以检查几个键的状态?

I'm using cocos2d for mac (1.0.1)

I have these two methods (which are similar to the apple ones)

//keyboard delegate is defined - these methods are called
- (BOOL)ccKeyDown:(NSEvent *)event {
    unichar key = [[event characters] characterAtIndex:0];
    if (key == NSDownArrowFunctionKey) {
        // Down arrow pressed
    }
    if(key == NSUpArrowFunctionKey) {
        // Up arrow pressed
        return YES;
    }
    if(key == NSLeftArrowFunctionKey) {
        // Left arrow pressed
       something = -1.0f;
        return YES;

    } else if(key == NSRightArrowFunctionKey) {
        // Right arrow pressed
       something = 1.0f;
        return YES;
    } 
    return NO;
}
-(BOOL)ccKeyUp:(NSEvent *)event {
    unichar key = [[event characters] characterAtIndex:0];
    if (key == NSDownArrowFunctionKey) {
        // Down arrow pressed
    }
    if(key == NSUpArrowFunctionKey) {
        // Up arrow pressed
    }
    if(key == NSLeftArrowFunctionKey || key == NSRightArrowFunctionKey) {
        // Left arrow pressed
       something = 0.0f;
    } 

    return YES;
}

I want to call this:

something = 0.0f;

if the left arrow key and the right arrow key are not pressed/hold. As far as I know ccKeyUp will only be called if a key was pressed and than released. And ccKeyDown if a key was pressed.

How can I get the current keyboard state to check a few keys for their states?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

草莓酥 2024-12-08 02:03:43

Cocos2D 不提供仅检查键盘按键状态的方法。

Kobold2D 可以。使用 Kobold2D,您可以执行以下操作:

KKInput* input = [KKInput sharedInput];
if ([input isKeyDown:KKKeyCode_LeftArrow] == NO && 
    [input isKeyDown:KKKeyCode_RightArrow] == NO)
{
    something = 0.0f;
}

此外,正如旁注,将字符用于键盘事件是不好的做法:

unichar key = [[event characters] characterAtIndex:0];

问题是用户的区域设置(键盘输入语言)会更改所需的键或组合键按下以生成该字符。例如,虽然 /(斜杠)键在美国键盘上位于右 Shift 键左侧,但在德语键盘上,用户必须按 Shift+7 才能执行映射到 / 字符的操作。您可以在我的关于正确处理 Mac 键盘事件<的文章中了解更多信息/a>.

Cocos2D does not offer a way to just check the states of keyboard keys.

Kobold2D does. With Kobold2D you can do something like this:

KKInput* input = [KKInput sharedInput];
if ([input isKeyDown:KKKeyCode_LeftArrow] == NO && 
    [input isKeyDown:KKKeyCode_RightArrow] == NO)
{
    something = 0.0f;
}

Also, just as a side note, it is bad practice to use the characters for keyboard events:

unichar key = [[event characters] characterAtIndex:0];

The problem with that is that the user's locale (keyboard input language) changes which key or key combination needs to be pressed to generate that character. For example while the / (slash) key is conveniently located on US Keyboards to the left of the Right Shift key, on german keyboards the users would have to press Shift+7 to execute the action that you've mapped to the / character. You can learn more about this in my article about Correctly Processing Mac Keyboard Events.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文