Created
September 25, 2012 16:42
-
-
Save GaretJax/3783042 to your computer and use it in GitHub Desktop.
HID Device to UDP
This file contains 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
#import <Foundation/Foundation.h> | |
#import <IOKit/hid/IOHIDManager.h> | |
#import <IOKit/hid/IOHIDUsageTables.h> | |
#define KEYS 4 | |
static void Handle_InputCallback(void *inContext, IOReturn inResult, void *inSender, IOHIDValueRef value) | |
{ | |
IOHIDElementRef elem = IOHIDValueGetElement(value); | |
uint16_t scancode = IOHIDElementGetUsage(elem); | |
if (scancode < 4 || scancode > 231) { | |
return; | |
} | |
NSLog(@"Key event received"); | |
} | |
static void Handle_DeviceMatchingCallback(void * inContext, IOReturn inResult, void * inSender, IOHIDDeviceRef inIOHIDDeviceRef) | |
{ | |
NSLog(@"Connected"); | |
} | |
static void Handle_RemovalCallback(void * inContext, IOReturn inResult, void * inSender, IOHIDDeviceRef inIOHIDDeviceRef) | |
{ | |
NSLog(@"Removed"); | |
} | |
int main(int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
IOHIDManagerRef manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDManagerOptionNone); | |
if (CFGetTypeID(manager) != IOHIDManagerGetTypeID()) { | |
exit(1); | |
} | |
int vendorId = 0x05ac; | |
//vendorId = 0x1234; | |
int productId = 0x0237; | |
//productId = 0x5678; | |
int usagePage = kHIDPage_GenericDesktop; | |
int usage = kHIDUsage_GD_Keyboard; | |
CFStringRef keys[KEYS] = { | |
CFSTR(kIOHIDVendorIDKey), | |
CFSTR(kIOHIDProductIDKey), | |
CFSTR(kIOHIDDeviceUsagePageKey), | |
CFSTR(kIOHIDDeviceUsageKey), | |
}; | |
CFNumberRef values[KEYS] = { | |
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vendorId), | |
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &productId), | |
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &usagePage), | |
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &usage), | |
}; | |
CFDictionaryRef matchingDict = CFDictionaryCreate(kCFAllocatorDefault, | |
(const void **) keys, (const void **) values, KEYS, | |
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); | |
for (int i=0; i<KEYS; i++) { | |
CFRelease(keys[i]); | |
CFRelease(values[i]); | |
} | |
IOHIDManagerSetDeviceMatching(manager, matchingDict); | |
CFRelease(matchingDict); | |
IOHIDManagerRegisterDeviceMatchingCallback(manager, Handle_DeviceMatchingCallback, null); | |
IOHIDManagerRegisterDeviceRemovalCallback(manager, Handle_RemovalCallback, null); | |
IOHIDManagerRegisterInputValueCallback(manager, Handle_InputCallback, null); | |
IOHIDManagerOpen(manager, kIOHIDOptionsTypeSeizeDevice); | |
IOHIDManagerScheduleWithRunLoop(manager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); | |
[[NSRunLoop mainRunLoop] run]; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment