Cocoa不会捕获移位修饰符? [英] Cocoa Won't Capture Shift Modifier?

查看:251
本文介绍了Cocoa不会捕获移位修饰符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,其中我试图捕获shift键修改器执行一个操作,但是当我运行程序,按下和普通键没有shift键修改器,我得到一个嘟声,修改符和键是不发送到我的keydown事件。相关代码是:

I have an application in which I'm trying to capture the shift key modifier to perform an action, however when I run the program and press and normal key without the shift key modifier I get a beep and the modifier and key are not sent to my keyDown event. The relevant code is:

NSString* eventChars = [theEvent charactersIgnoringModifiers];

if ([eventChars isEqualTo:@"w"]) {
    newPlayerRow++;
    direction = eUp;
} else if ([eventChars isEqualTo:@"x"]) {
    newPlayerRow--;
    direction = eDown;
} else if ([eventChars isEqualTo:@"a"]) {
    newPlayerCol--;
    direction = eLeft;
} else if ([eventChars isEqualTo:@"d"]) {
    newPlayerCol++;
    direction = eRight;
} else {
    [super keyDown:theEvent];
    return;
}

// handle the player firing a bullet
if (([theEvent modifierFlags] & (NSShiftKeyMask | NSAlphaShiftKeyMask)) != 0) {
    NSLog(@"Shift key");
    [self fireBulletAtColumn:newPlayerCol row:newPlayerRow inDirection:direction];
    [self setNeedsDisplay:YES];
} else {
    ...
}

不知道是什么原因造成这种,但我想要能够捕获shift键按。感谢您对此问题的任何帮助。

I'm not sure what is causing this, but I'd like to be able to capture shift key presses. Thanks in advance for any help with this problem.

编辑:如果有任何区别,我也使用MacBook键盘。

Also I'm using a MacBook keyboard if that makes any difference.

编辑:这绝对是一个以移位为中心的问题,因为将NSShiftKeyMask | NSAlphaShiftKeyMask改为NSControlKeyMask会产生所需的效果。

This is definitely a shift-centric problem as changing (NSShiftKeyMask | NSAlphaShiftKeyMask) to NSControlKeyMask does have the desired effect.

推荐答案

首先,-charactersIgnoringModifiers不忽略shift键,所以你仍然会得到从它返回的移位字符(即UPPERCASE和!%#$%^& *)。你的函数中可能发生的是:你按shift-w,你的-isEqualTo:返回false,因为你正在比较小写字母'w'和大写'W',所以你在返回之前到移动检测代码底端。最简单的解决方案是只检查两者。

First, -charactersIgnoringModifiers doesn't ignore the shift key, so you will still get shifted characters (i.e UPPERCASE and !%#$%^&*) returned from it. What's probably happening in your function is: You press shift-w, your -isEqualTo: returns false because you're comparing a lowercase 'w' and an uppercase 'W', and so you return before getting to the shift-detection code at the bottom. The simplest solution is to just check for both.

但是,如果你想,例如,阿拉伯键盘手能够轻松使用你的应用程序,你真的不应该硬编码字符,甚至可能不会出现在用户的键盘上。 -keyCode返回的值指的是键在键盘上的位置,而不是表示的字符。对于初学者,可以将Events.h中的kVK_ANSI_和kVK_(您可能必须链接到Carbon.framework和#include< Carbon / Carbon.h>以使用那些常量)开始的常数与什么 - keyCode返回,它们引用QWERTY使用的预期的键位置。所以你可以确定,不管键盘布局,'wasd'(kVK_ANSI_W,kVK_ANSI_A等)的键码将引用用户键盘左上角的三角形。

However, if you want, for example, Arabic keyboardists to be able to easily use your app, you really shouldn't hardcode characters that may not even appear on the user's keyboard. The value returned by -keyCode refers to a key's position on the keyboard, not the represented character. For starters, the constants beginning in 'kVK_ANSI_' and 'kVK_' in Events.h (you may have to link to Carbon.framework and #include <Carbon/Carbon.h> to use those constants) can be compared to what -keyCode returns, and they refer to the key positions a QWERTY-using USian expects. So you can be (pretty) sure that, regardless of keyboard layout, the keycodes for 'wasd' (kVK_ANSI_W, kVK_ANSI_A, etc.) will refer to that triangle in the top left of your user's keyboard.

这篇关于Cocoa不会捕获移位修饰符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆