Guest User

Untitled

a guest
Nov 18th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. // Usage:
  2. // Basically, there are two ways to use this, either just invoke it via
  3. // modifierKeyState
  4. // and the exit code will be a bit mask for the state of the modifier keys,
  5. // or:
  6. // modifierKeyState command
  7. // modifierKeyState shift alt
  8. // exit code 0 means the specified keys (and only those!) have been found
  9. // exit code 1 means the key was either not pressed, or other keys have been pressed as well
  10. // Lastly:
  11. // modifierKeyState null
  12. // will check if NO modifier key has been pressed (exit code will be either 0 or 1, in contrast to invoking w/o the argument)
  13.  
  14. // clang -framework Foundation -framework ApplicationServices -o modifierKeyState modifierKeyState.m
  15.  
  16. #import <Foundation/Foundation.h>
  17.  
  18. enum {
  19. // EXIT_SUCCESS = 0,
  20. // EXIT_FAILURE = 1,
  21. EXIT_CAPSLOCK = 1 << 1,
  22. EXIT_SHIFT = 1 << 2,
  23. EXIT_CONTROL = 1 << 3,
  24. EXIT_ALTERNATE = 1 << 4,
  25. EXIT_COMMAND = 1 << 5,
  26. EXIT_HELP = 1 << 6,
  27. EXIT_FN = 1 << 7
  28. };
  29.  
  30. int main(int argc, const char * argv[])
  31. {
  32. int exitCode = 0;
  33.  
  34. @autoreleasepool {
  35. CGEventFlags flags = CGEventSourceFlagsState(kCGEventSourceStateHIDSystemState);
  36.  
  37. if( kCGEventFlagMaskAlphaShift & flags ) { exitCode |= EXIT_CAPSLOCK; }
  38. if( kCGEventFlagMaskShift & flags ) { exitCode |= EXIT_SHIFT; }
  39. if( kCGEventFlagMaskControl & flags ) { exitCode |= EXIT_CONTROL; }
  40. if( kCGEventFlagMaskAlternate & flags ) { exitCode |= EXIT_ALTERNATE; }
  41. if( kCGEventFlagMaskCommand & flags ) { exitCode |= EXIT_COMMAND; }
  42. if( kCGEventFlagMaskHelp & flags ) { exitCode |= EXIT_HELP; }
  43. if( kCGEventFlagMaskSecondaryFn & flags ) { exitCode |= EXIT_FN; }
  44.  
  45. // at this point, the exit code is a bit mask for the keyboard modifier state
  46.  
  47. NSArray *modifierNames = [NSArray arrayWithObjects:@"null", @"capslock", @"shift", @"control", @"option", @"command", @"help", @"fn", nil];
  48.  
  49. for (int i = 1; i < argc; i++) {
  50. NSString *arg = [[[NSString alloc] initWithCString:argv[i]
  51. encoding:NSUTF8StringEncoding] lowercaseString];
  52. NSUInteger idx = [modifierNames indexOfObject:arg];
  53.  
  54. if (idx == 0) {
  55. // nothing to do for "null" argument
  56. continue;
  57. }
  58. if (idx == NSNotFound) {
  59. exitCode = EXIT_FAILURE;
  60. NSLog(@"invalid argument: %@", arg);
  61. break;
  62. }
  63.  
  64. // modifierNames idx matches EXIT_* bit shift, as the exitCode is a bit mask,
  65. // the relevant bit is XOR-ed, meaning that if the bit was set (i.e. key pressed)
  66. // it gets set to 0 -- thus, iff only the keys from the command line have been pressed
  67. // we will end up with an exitCode of 0
  68. exitCode ^= (1 << idx);
  69. }
  70.  
  71. if ( (argc > 1) && (exitCode > 0) )
  72. {
  73. // so either a key was pressed the user did not want, or he asked for a key that wasn't pressed
  74. exitCode = EXIT_FAILURE;
  75. }
  76. }
  77.  
  78. return exitCode;
  79. }
Add Comment
Please, Sign In to add comment