detect command key status

  • What is the best way to detect the command key down / up?

    There must be something more elegant then:

    - (void) flagsChanged:(NSEvent *)theEvent
    {
        if([theEvent modifierFlags] == 1048840 | [theEvent
    modifierFlags] == 1048584 | [theEvent modifierFlags] == 1048848){
            commandKeyDown = YES;
        }
        if([theEvent modifierFlags] == 256){
            commandKeyDown = NO;
        }
    }
  • Thanks for your suggestions.  Not sure why the NSCommandKeyMask does
    not cover my command keys though.
    I have to check for other modifierflags.

    - (void) flagsChanged:(NSEvent *)theEvent {
        int modifierflag = [theEvent modifierflag];

        if( modifierflag == NSCommandKeyMask || modifierflag == 1048840
    || modifierflag == 1048584 || modifierflag == 1048848) {
            commandKeyDown = YES;
        }else if(modifierflag == 256){
            commandKeyDown = NO;
        }
    }

    >> What is the best way to detect the command key down / up?
    >>
    >> There must be something more elegant then:
    >>
    >> - (void) flagsChanged:(NSEvent *)theEvent
    >> {
    >> if([theEvent modifierFlags] == 1048840 | [theEvent
    >> modifierFlags] == 1048584 | [theEvent modifierFlags] == 1048848){
    >> commandKeyDown = YES;
    >> }
    >> if([theEvent modifierFlags] == 256){
    >> commandKeyDown = NO;
    >> }
    >> }
    >>
    >
    > Some random suggestions:
    >
    > - save the [theEvent modifierFlags] result in a variable to avoid
    > calling it 4 times.
    >
    > - use || instead of |.
    >
    > - add a else between the 2 if
    >
    > - use constants.
    >
    > - don't mix 2 {} styles.
    >
    > My $0.02
    >
  • On Jun 27, 2005, at 10:07 AM, Richard Salvatierra wrote:

    > Thanks for your suggestions.  Not sure why the NSCommandKeyMask
    > does not cover my command keys though.
    > I have to check for other modifierflags.
    >
    >
    > - (void) flagsChanged:(NSEvent *)theEvent {
    > int modifierflag = [theEvent modifierflag];
    >
    > if( modifierflag == NSCommandKeyMask || modifierflag == 1048840
    > || modifierflag == 1048584 || modifierflag == 1048848) {
    > commandKeyDown = YES;
    > }else if(modifierflag == 256){
    > commandKeyDown = NO;
    > }
    > }
    >
    >

    Why not just check the specific bit instead of all possible
    variations of other modifier keys?  Since modifier flag contains the
    state of _all_ modifiers (not just the command key) you really only
    want to look at the one bit...

        commandKeyDown = (modifierFlag & NSCommandKeyMask) ==
    NSCommandKeyMask;

    Glenn Andreas                      <gandreas...>
      <http://www.gandreas.com/> wicked fun!
    Widgetarium | the quickest path to widgets
  • This is a bitmask, so you should be using something like this:

        BOOL hasCmdKeyMask = ((theEvent modifierflag] &
    NSCommandKeyMask) != 0);

    (You might be noticing the effect of the "defice-dependent bits". See
    the docs for more info.
    On Mac OS X 10.4 you can use the NSDeviceIndependentModifierFlagsMask
    to work around that particular problem, if it happens to be what is
    confusing in this case.)

    j o a r
previous month june 2005 next month
MTWTFSS
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      
Go to today