I’m attempting to seize focus on a USB card reader such that only my program interacts with the card read events. With the code below, it is registering when the card reader is connected or removed correctly, but it does not log card read events. I know they are happening though, because the machine makes a noise for each character read.
I got most of this code from here
#import <Foundation/Foundation.h>
#import <IOKit/hid/IOHIDManager.h>
#import <IOKit/hid/IOHIDUsageTables.h>
#include <IOKit/hid/IOHIDValue.h>
#define KEYS 4
const char* keyboard_map(int scancode)
{
//char * letter;
switch (scancode) {
case 0x04: return "a";
case 0x05: return "b";
case 0x06: return "c";
case 0x07: return "d";
case 0x08: return "e";
case 0x09: return "f";
case 0x0A: return "g";
case 0x0B: return "h";
case 0x0C: return "i";
case 0x0D: return "j";
case 0x0E: return "k";
case 0x0F: return "l";
case 0x10: return "m";
case 0x11: return "n";
case 0x12: return "o";
case 0x13: return "p";
case 0x14: return "q";
case 0x15: return "r";
case 0x16: return "s";
case 0x17: return "t";
case 0x18: return "u";
case 0x19: return "v";
case 0x1A: return "w";
case 0x1B: return "x";
case 0x1C: return "y";
case 0x1D: return "z";
case 0x1E: return "1";
case 0x1F: return "2";
case 0x20: return "3";
case 0x21: return "4";
case 0x22: return "5";
case 0x23: return "6";
case 0x24: return "7";
case 0x25: return "8";
case 0x26: return "9";
case 0x27: return "0";
case 0x28: return "Return (Enter)";
default:
return "";
}
return "";
}
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;
printf("%sn", keyboard_map(scancode));
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 = 0x0C27;
//vendorId = 0x1234;
int productId = 0x3BFA;
//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);
IOHIDManagerScheduleWithRunLoop(manager, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
IOHIDManagerOpen(manager, kIOHIDOptionsTypeSeizeDevice);
CFRunLoopRun();
//[[NSRunLoop mainRunLoop] run];
}
return 0;
}
Ideally I would like to store all characters read before a return (0x028) in a string, but first and formost I’d like to actually be able to interact with the characters being read.