Duck
Duck

Reputation: 36013

Detecting NSKeyUp of the Shift key

I am using this to detect keystrokes on my app...

[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown
                                    handler:^NSEvent * (NSEvent * theEvent)

OK I can use theEvent to know what characters were typed and know if a shift was pressed using this:

NSString *typedKey = theEvent.charactersIgnoringModifiers;
BOOL shiftDetected = [theEvent modifierFlags] & NSShiftKeyMask;

My App has an interface displayed with some buttons and I am allowing the keyboard to be used instead of clicking on the buttons. This interface has 3 buttons in particular that has a second function.

For example: the first button has 2 functions, A and B but just the A label is displayed on that button. Lets say I specify that the letter Q is the keyboard shortcut for that button. If the user presses Q function A is executed. If the user presses Shift Q then function B is executed.

But this is the problem. I need to detect all presses or releases of the Shift, because the moment the user presses Shift I have to change the label of that button from A to B, so the user knows that now that button will lead to the execution of function B instead of A. Like a keyboard that will change from lowercase to uppercase while Shift is being hold and change back to lowercase the moment Shift is released.

How do I do that?

Upvotes: 0

Views: 1315

Answers (1)

brianLikeApple
brianLikeApple

Reputation: 4371

I created a simple project using addLocalMonitorForEvents function. Please check my code, it is Swift code but I think it should be as same as objective c.

func applicationDidFinishLaunching(_ aNotification: Notification) {
    // Insert code here to initialize your application
    NSEvent.addLocalMonitorForEvents(matching: [.flagsChanged, .keyDown]) { (theEvent) -> NSEvent? in
        if theEvent.modifierFlags.contains(.shift) {
            if theEvent.keyCode == 56 { // this is Shif key
                print("Shift START!")
            }
            else {
                print("Shift pressed with keycode \(theEvent.keyCode)")
            }
        }
        else {
            if theEvent.keyCode == 56 { // this is Shif key
                print("Shift END!")
            }
            else {
                print("Normal keycode \(theEvent.keyCode)")
            }
        }
        return theEvent
    }
}

This is Objective c:

[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskFlagsChanged|NSEventMaskKeyDown handler:^NSEvent * (NSEvent * theEvent) {
    if ([theEvent modifierFlags] & NSEventModifierFlagShift) {
        if (theEvent.keyCode == 56) { // this is Shif key
            NSLog(@"Shift START");
        }
        else {
            NSLog(@"Shift pressed with keycode %d", theEvent.keyCode);
        }
    }
    else {
        if (theEvent.keyCode == 56) { // this is Shif key
            NSLog(@"Shift END");
        }
        else {
            NSLog(@"Normal keycode %d", theEvent.keyCode);
        }
    }

    return theEvent;
}];

Just copy and paste this part to your AppDelegate for quick testing.

enter image description here

Upvotes: 8

Related Questions