forked from supermari0/WizardKeys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWizardKeys.c
44 lines (41 loc) · 1.43 KB
/
WizardKeys.c
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* WizardKeys.c
*
* This program turns the keyboard backlight on and off with each key press.
*
* Compile with gcc -o WizardKeys WizardKeys.c -framework IOKit -framework
* ApplicationServices -framework CoreFoundation
*
* By Mario Villaplana http://www.stanford.edu/~mariojv
*
* Use at your own risk; I'm not responsible if your keyboard lights burn out from
* too much use.
*/
#include "MacKeyLEDAPI.h"
#include <ApplicationServices/ApplicationServices.h>
CGEventRef lightCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef
event, void *refcon) {
if (getKeyboardLEDValue() < 0xfff / 2) {
setKeyboardLEDValue(0xfff);
} else {
setKeyboardLEDValue(0);
}
return NULL; // Returns NULL since passive listeners do not affect stream.
}
int main(int argc, const char * argv[]) {
startLightService();
CGEventMask keyMask = CGEventMaskBit(kCGEventKeyDown);
CFMachPortRef eventTap = CGEventTapCreate(kCGSessionEventTap,
kCGTailAppendEventTap, kCGEventTapOptionListenOnly, keyMask,
lightCallback, NULL);
if (eventTap == NULL) {
fprintf(stderr, "Event tap could not be created. Ending program.");
exit(1);
}
CFRunLoopSourceRef runLoopSource =
CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource,
kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true); // needed? should be enabled by default
CFRunLoopRun();
return 0;
}