Created
March 25, 2017 20:07
-
-
Save cieslak/709fd8d24342039a0c23e7c508e7920a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// kbfuck.m | |
// | |
// randomly remaps all keys on keyboard in macOS | |
#import <Foundation/Foundation.h> | |
#import <IOKit/hidsystem/IOHIDEventSystemClient.h> | |
#import <IOKit/hidsystem/IOHIDServiceClient.h> | |
#import <IOKit/hid/IOHIDUsageTables.h> | |
int main(int argc, const char * argv[]) { | |
IOHIDEventSystemClientRef system; | |
CFArrayRef services; | |
NSMutableArray *map = [[NSMutableArray alloc] init]; | |
for (int i = 0; i < 100; i++) { | |
uint64_t code1 = (arc4random_uniform(0x6f) + 0x4) | 0x700000000; | |
uint64_t code2 = (arc4random_uniform(0x6f) + 0x4) | 0x700000000; | |
NSDictionary *map1 = @{@kIOHIDKeyboardModifierMappingSrcKey:@(code1), @kIOHIDKeyboardModifierMappingDstKey:@(code2)}; | |
NSDictionary *map2 = @{@kIOHIDKeyboardModifierMappingSrcKey:@(code2), @kIOHIDKeyboardModifierMappingDstKey:@(code1)}; | |
[map addObject:map1]; | |
[map addObject:map2]; | |
} | |
system = IOHIDEventSystemClientCreateSimpleClient(kCFAllocatorDefault); | |
services = IOHIDEventSystemClientCopyServices(system); | |
for(CFIndex i = 0; i < CFArrayGetCount(services); i++) { | |
IOHIDServiceClientRef service = (IOHIDServiceClientRef)CFArrayGetValueAtIndex(services, i); | |
if(IOHIDServiceClientConformsTo(service, kHIDPage_GenericDesktop, kHIDUsage_GD_Keyboard)) { | |
IOHIDServiceClientSetProperty(service, CFSTR(kIOHIDUserKeyUsageMapKey), (CFArrayRef)map); | |
} | |
} | |
CFRelease(services); | |
CFRelease(system); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment