P i*_*P i 5 macos hook tap iokit keyboard-events
我现在已经发现如何在OS X上以低级别挂机/点击键盘事件:如何在MacBook键盘上点击(挂钩)F7到F12和Power/Eject
从该答案中打印出代码:
// compile and run from the commandline with:
// clang -framework coreFoundation -framework IOKit ./HID.c -o hid
// sudo ./hid
// This code works with the IOHID library to get notified of keys.
// Still haven't figured out how to truly intercept with
// substitution.
#include <IOKit/hid/IOHIDValue.h>
#include <IOKit/hid/IOHIDManager.h>
void myHIDKeyboardCallback( void* context, IOReturn result, void* sender, IOHIDValueRef value )
{
IOHIDElementRef elem = IOHIDValueGetElement( value );
if (IOHIDElementGetUsagePage(elem) != 0x07)
return;
uint32_t scancode = IOHIDElementGetUsage( elem );
if (scancode < 4 || scancode > 231)
return;
long pressed = IOHIDValueGetIntegerValue( value );
printf( "scancode: %d, pressed: %ld\n", scancode, pressed );
}
CFMutableDictionaryRef myCreateDeviceMatchingDictionary( UInt32 usagePage, UInt32 usage )
{
CFMutableDictionaryRef dict = CFDictionaryCreateMutable(
kCFAllocatorDefault, 0
, & kCFTypeDictionaryKeyCallBacks
, & kCFTypeDictionaryValueCallBacks );
if ( ! dict )
return NULL;
CFNumberRef pageNumberRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, & usagePage );
if ( ! pageNumberRef ) {
CFRelease( dict );
return NULL;
}
CFDictionarySetValue( dict, CFSTR(kIOHIDDeviceUsagePageKey), pageNumberRef );
CFRelease( pageNumberRef );
CFNumberRef usageNumberRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, & usage );
if ( ! usageNumberRef ) {
CFRelease( dict );
return NULL;
}
CFDictionarySetValue( dict, CFSTR(kIOHIDDeviceUsageKey), usageNumberRef );
CFRelease( usageNumberRef );
return dict;
}
int main(void)
{
IOHIDManagerRef hidManager = IOHIDManagerCreate( kCFAllocatorDefault, kIOHIDOptionsTypeNone );
CFArrayRef matches;
{
CFMutableDictionaryRef keyboard = myCreateDeviceMatchingDictionary( 0x01, 6 );
CFMutableDictionaryRef keypad = myCreateDeviceMatchingDictionary( 0x01, 7 );
CFMutableDictionaryRef matchesList[] = { keyboard, keypad };
matches = CFArrayCreate( kCFAllocatorDefault, (const void **)matchesList, 2, NULL );
}
IOHIDManagerSetDeviceMatchingMultiple( hidManager, matches );
IOHIDManagerRegisterInputValueCallback( hidManager, myHIDKeyboardCallback, NULL );
IOHIDManagerScheduleWithRunLoop( hidManager, CFRunLoopGetMain(), kCFRunLoopDefaultMode );
IOHIDManagerOpen( hidManager, kIOHIDOptionsTypeNone );
CFRunLoopRun(); // spins
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能(可能调整该代码)识别哪个键盘负责特定事件?
用例是我计划使用外部键盘重新映射,但同时保留内置MacBook键盘的原始映射.
编辑:
辅助键盘的OSX HID过滤器?
https://github.com/candera/khordr/blob/master/src/c/keygrab/hid-scratch.c
http://ianjoker.googlecode.com/svn/trunk/Joker/Joker/hid_test.cpp
http: //www.cplusplusdevelop.com/72_17345226/
http://www.cocoabuilder.com/archive/cocoa/229902-which-keyboard-barcode-scanner-did-the-event-come-from.html
如果您分别在每个感兴趣的设备上注册回调,IOHIDDeviceRegisterInputValueCallback则sender参数将IOHIDDeviceRef指示设备。(而不是使用IOHIDManagerRegisterInputValueCallback发送者将是 HID 管理器引用的位置)
唯一的缺点是您需要注册并处理匹配设备的热插拔事件的通知。(每当新设备出现时注册,当设备消失时注销)
您可以使用IOHIDDeviceCreate()- 这将 anio_service_t作为参数来获取 HID 设备引用。这反过来意味着您需要使用标准 IOKit IOService 匹配函数来获取和监视您的设备列表,但您确实获得了单个设备的显式列表,您可以查询该列表以向用户显示名称等。其关键功能是IOServiceAddMatchingNotification。