2

I wanna intercept hotkeys that begin with Control+Shift and ends with a character (mandatory).
I have the following code:

[NSEvent addGlobalMonitorForEventsMatchingMask:NSFlagsChangedMask handler: ^(NSEvent *event) {
    NSUInteger flags = [event modifierFlags] & NSDeviceIndependentModifierFlagsMask;
    if(flags == NSControlKeyMask + NSShiftKeyMask){
        NSLog(@"pressed!");
    }
}];

What do i need to add to my code to check if the user pressed ControlShift+character, and what character the user pressed?
The code NSLog(@"pressed!"); will be executed only if what i said above is true.

This is my pseudo-code for what i'm looking for:

[NSEvent addGlobalMonitorForEventsMatchingMask:NSFlagsChangedMask handler: ^(NSEvent *event) {
    NSUInteger flags = [event modifierFlags] & NSDeviceIndependentModifierFlagsMask;
    if((flags == NSControlKeyMask + NSShiftKeyMask) && [event containsCharacter]){
       NSLog(@"%@", [event character];
    }
}];

So if the user presses Control+Shift+1 i'll do one thing, if Control+Shift+2 other thing, and so on...

Pedro Vieira
  • 3,330
  • 3
  • 41
  • 76
  • possible duplicate of [Check modifierFlags of NSEvent if a certain modifier was pressed but no other](http://stackoverflow.com/questions/6084266/check-modifierflags-of-nsevent-if-a-certain-modifier-was-pressed-but-no-other) – jscs Sep 12 '12 at 01:37
  • It's a bitmask. Check it like any other. – jscs Sep 12 '12 at 01:38
  • can you please answer the question with example code for what i've asked so i can accept your answer, please? – Pedro Vieira Sep 12 '12 at 03:16
  • 2
    I'm not going to repost the answer from that question. If the link that I provided has answered your question, then click the "flag" link and write a message indicating that. Otherwise, make it clearer what you're asking and how it differs from the link I've provided. – jscs Sep 12 '12 at 04:42
  • i just want to know if the event was triggered by ctrl-shift and a normal character. for example, take a look at this pseudo-code (based on what i wrote above): `if((flags == NSControlKeyMask + NSShiftKeyMask) && [event containsCharacter]){ NSLog(@"%@", [event character];}` understand? – Pedro Vieira Sep 12 '12 at 19:02
  • In order to capture global keystrokes, you will also need to enable access for assistive devices in the Accessibility menu of system preferences.. – Eric Sep 24 '13 at 15:18

2 Answers2

3

You need to compare bitwise:

- (void)keyDown:(NSEvent *)theEvent { 
    if ([theEvent modifierFlags] & (NSControlKeyMask | NSShiftKeyMask)) { 
        if (theEvent.keyCode == 1/* add the right key code */) {
            NSLog(@"Do something");
        }
    } else { 
        [super keyDown:theEvent]; 
    } 
} 
IluTov
  • 6,807
  • 6
  • 41
  • 103
  • hai @Pedro Viera, can you tell me where to call this keyDown: method? – R. Dewi Sep 20 '12 at 05:52
  • @R. Dewi, just use this on `applicationDidFinishLaunching:` - `[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler: ^(NSEvent *event) { NSUInteger flags = [event modifierFlags] & NSDeviceIndependentModifierFlagsMask; if (flags == NSControlKeyMask + NSShiftKeyMask) { //code if user presses ctrl+shift } }]; ` – Pedro Vieira Sep 20 '12 at 09:59
  • thank you for the answer @PedroVieira , can i do something like this for iPad bluetooth keyboard? – R. Dewi Sep 20 '12 at 11:01
-3

Try this:

 [NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *event) {
    NSUInteger key = 8; // 8 is "C"
    NSUInteger modifier = NSControlKeyMask + NSShiftKeyMask; 
    if ([event keyCode] == key && [NSEvent modifierFlags] == modifier)

NSLog(@"pressed!");

}];
Ramaraj T
  • 5,184
  • 4
  • 35
  • 68
  • 3
    This is not how you check bitmasks. There are extra flags in `modifierFlags` that will ensure that it is never `==` your `modifier`. – jscs Sep 12 '12 at 18:10