如何获取mac键盘上某些按键的状态?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Cocos2D 不提供仅检查键盘按键状态的方法。
Kobold2D 可以。使用 Kobold2D,您可以执行以下操作:
此外,正如旁注,将字符用于键盘事件是不好的做法:
问题是用户的区域设置(键盘输入语言)会更改所需的键或组合键按下以生成该字符。例如,虽然 /(斜杠)键在美国键盘上位于右 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:
Also, just as a side note, it is bad practice to use the characters for keyboard events:
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.