Cocoa Will not reset shift modifier?

I have an application in which I try to grab the shift key modifier to perform an action, however, when I run the program and press the normal key without the shift key modifier, I get a beep and the modifier and key are not sent to the keyDown event. Relevant Code:

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 {
    ...
}

I'm not sure what causes this, but I would like to be able to record shift keystrokes. Thanks in advance for any help in resolving this issue.

EDIT: I also use a MacBook keyboard, if that matters.

EDIT: This is definitely a shift issue because changing (NSShiftKeyMask | NSAlphaShiftKeyMask) to NSControlKeyMask has the desired effect.

+3
5

-, -charactersIgnoringModifiers shift, (, UPPERCASE !% # $% ^ & *). , : shift-w, -isEqualTo: false, "w" " " , , . - .

, , , , , , . , -keyCode, , . , "kVK_ANSI_" "kVK_" Events.h( Carbon.framework #include < Carbon/Carbon.h > ) , keyCode , , QWERTY- USian. , , , , "wasd" (kVK_ANSI_W, kVK_ANSI_A ..) .

+5

, NSResponder flags.

- (void)flagsChanged:(NSEvent *)theEvent

+4

eventChars modifierFlags NSLog? , , . , , - .

NSLog(@"Chars: %@, modifier flags: 0x%x", eventChars, [theEvent modifierFlags]);

:

  • isEqualTo: - AppleScript. , , compare:. - isEqualToString: isEqual:, .
  • QWERTY - wsad, wxad. -T-. , T.

    , , , , (, Dvorak ..) . , Inside Macintosh, -. .

  • "fireBullet From Column:row:inDirection:"? , , , ( , , ), ; "At" "From", .
+3

, , , . , "w, x, a, d", (. else).

, "[super keyDown: theEvent]; return;" keyDown.

+1

:

  • , , -charactersIgnoringModifiers Shift ( documentation) , Shift . Control, Shift - . lowercase :

    eventChars = [[theEvent charactersIgnoringModifiers] lowercaseString];
    
  • ( ) isEqualTo: isEqualToString: ( , , ).

:

, ( ), .

Create a view in your preferences using the input for each command and write down the key code for the key that the user presses. Save them to NSUserDefaults and check them instead of your hard-coded strings. You can create a default set for your current "wasd" keys.

See examples, such as the Recorder Shortcut, for examples of how to do this (or search for friend ).

0
source

All Articles