Command-Key-Up Cocoa

I'm trying to imitate the functionality of the cmd-tab keyboard shortcut, where the user can switch between applications that click on a specific key, and then when they release the command, something happens.

I am using this code right now, but it can only detect keydown. I need this to shoot the keys

- (void)flagsChanged:(NSEvent *)theEvent { if ([theEvent modifierFlags] & NSCommandKeyMask) { NSLog(@"Do my stuff here"); } } 

thanks

+4
source share
1 answer

According to the docs:

Tells the recipient that the user pressed or released the modifier (Shift, Control, etc.).

What you need to do when you receive an event in which the command key is reset, you need to set the flag somewhere, and in subsequent calls - check the absence of the pressed command key.

For example, if you have ivar called _cmdKeyDown :

 - (void)flagsChanged:(NSEvent *)theEvent { [super flagsChanged:theEvent]; NSUInteger f = [theEvent modifierFlags]; BOOL isDown = !!(f & NSCommandKeyMask); if (isDown != _cmdKeyDown) { NSLog(@"State changed. Cmd Key is: %@", isDown ? @"Down" : @"Up"); _cmdKeyDown = isDown; } } 
+9
source

Source: https://habr.com/ru/post/1485182/


All Articles