OSX监听全局键盘按下事件并捕获事件源的硬件接口位置
在OSX系统全局监听键盘的按下事件,并可以捕获事件源的硬件的接口位置,用于区分是哪个键盘产生的事件。下面的代码只是以键盘为例子,其实是可以适用于其他输入外设的。如有需要可搜索相关外设的匹配字典的创建代码。
设备匹配字典
CFMutableDictionaryRef myCreateDeviceMatchingDictionary(UInt32 usagePage, UInt32 usage )
{
CFMutableDictionaryRef dict = CFDictionaryCreateMutable(
kCFAllocatorDefault,
, & 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;
}
事件的回调函数
void myHIDKeyboardCallback(void* context, IOReturn result, void* sender, IOHIDValueRef value )
{
//get device product id and vendor id
IOHIDDeviceRef device = sender;
// int32_t pid = 1;
// CFNumberGetValue(IOHIDDeviceGetProperty(device, CFSTR(kIOHIDVendorIDKey)), kCFNumberSInt32Type, &pid);
// int32_t vendorID = 0;
// CFNumberGetValue(IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductIDKey)), kCFNumberSInt32Type, &vendorID);
// NSLog(@"vendor:%d product:%d", vendorID, pid); IOHIDElementRef elem = IOHIDValueGetElement( value ); // if (IOHIDElementGetUsagePage(elem) != 0x07)
// return; uint32_t scancode = IOHIDElementGetUsage( elem ); if (scancode < || scancode > )
return; int32_t locationID = ;
CFNumberGetValue(IOHIDDeviceGetProperty(device, CFSTR(kIOHIDLocationIDKey)), kCFNumberSInt32Type, &locationID);
NSLog(@"location:%d", locationID); long pressed = IOHIDValueGetIntegerValue( value );
uint64_t timestamp = IOHIDValueGetTimeStamp(value);
printf( "scancode: %3d, %s timestamp:%llu \n", scancode, pressed?"keydown":"keyup ", timestamp); //send the keyboard event
NSDictionary* userInfo = [[NSDictionary alloc]initWithObjectsAndKeys:
[NSNumber numberWithInt:scancode], NOTIFICATION_KEY_ScanCode,
[NSNumber numberWithInt:locationID], NOTIFICATION_KEY_Location,
[NSNumber numberWithLong:pressed], NOTIFICATION_KEY_IsKeyDown,
[NSNumber numberWithLong:timestamp], NOTIFICATION_KEY_Timestamp, nil];
NSNotification* notification = [NSNotification notificationWithName:NOTIFICATION_KeyBoardEvent object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:notification]; UsbSignalController* controller = (__bridge UsbSignalController*)context;
[controller onKeyEvent:scancode Location:locationID IsDown:pressed Timestamp:timestamp];
}
注册键盘钩子
-(void)registerKeyboardHook
{
IOHIDManagerRef hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone ); CFMutableDictionaryRef keyboard = myCreateDeviceMatchingDictionary( 0x01, );
CFArrayRef matches;
{
//CFMutableDictionaryRef keypad = myCreateDeviceMatchingDictionary( 0x01, 7 );
CFMutableDictionaryRef matchesList[] = { keyboard/*, keypad*/ };
matches = CFArrayCreate( kCFAllocatorDefault, (const void **)matchesList, /**/, NULL );
} IOHIDManagerSetDeviceMatchingMultiple(hidManager, matches );
CFRelease(matches);
CFRelease(keyboard ); IOHIDManagerRegisterInputValueCallback(hidManager, myHIDKeyboardCallback, (__bridge void*)self);
IOHIDManagerScheduleWithRunLoop(hidManager, CFRunLoopGetMain(), kCFRunLoopDefaultMode );
IOHIDManagerOpen(hidManager, kIOHIDOptionsTypeNone );
}
OSX监听全局键盘按下事件并捕获事件源的硬件接口位置的更多相关文章
- Flex 自动获取焦点 监听全局键盘事件
在mxml里监听addedToStage事件 protected function application1_addedToStageHandler(event:Event):void { this. ...
- Python——pyHook监听鼠标键盘事件
pyHook包为Windows中的全局鼠标和键盘事件提供回调. 底层C库报告的信息包括事件的时间,事件发生的窗口名称,事件的值,任何键盘修饰符等. 而正常工作需要pythoncom等操作系统的API的 ...
- WPF - 监听判断键盘组合键的按下
对于键盘事件PreviewKeyDown.PreviewKeyUp.KeyDown.KeyUp,在其中检查当次事件是哪个按键触发的很简单,只需要判断KeyEventArgs类型的事件参数e的Key属性 ...
- Android 另类方法监听软键盘的弹出收起事件
http://www.cnblogs.com/csonezp/p/5065624.html 最近做的项目碰到个问题,a界面是fragment+recyclerview,b界面带个edittext,并且 ...
- KeyDown/PreviewKeyDown事件中监听Alt键按下
一个坑 在WPF应用程序(或者其他Windows应用程序中),为了监听Alt键按下,我们可以尝试写出这样的代码: PreviewKeyDown += (s, e) => { if (e.Key ...
- adnroid 监听软键盘的显隐
首先注明出处,我只是想做个笔记:http://www.jb51.net/article/64820.htm 第一眼看到的时候,觉得不太可靠,因为之前都是看到这样处理的,根据监听一个根布局的size,可 ...
- Android App监听软键盘按键的三种方式
前言: 我们在android手机上面有时候会遇到监听手机软键盘按键的时候,例如:我们在浏览器输入url完毕后可以点击软键盘右下角的“GO”按键加载url页面:在点击搜索框的时候,点击右下角的sea ...
- 键盘-App监听软键盘按键的三种方式
前言: 我们在android手机上面有时候会遇到监听手机软键盘按键的时候,例如:我们在浏览器输入url完毕后可以点击软键盘右下角的“GO”按键加载url页面:在点击搜索框的时候,点击右下角的sea ...
- Android App监听软键盘按键的三种方式(转)
最近有类似需求,在csdn上刚好发现,粘贴过来,以防止忘记喽 前言: 我们在android手机上面有时候会遇到监听手机软键盘按键的时候,例如:我们在浏览器输入url完毕后可以点击软键盘右下角的“G ...
随机推荐
- Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性
简介 Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很 ...
- Tp框架中模板中if条件如何使用?
- js 替换json对象中的键名
js 替换json对象中的键名 CreateTime--2018年3月30日15:38:50 Author:Marydon 情景描述: 有个json数组,现在需要将json对象中的key替换掉,值 ...
- 【Django】TemplateDoesNotExist at /login/
在Django项目中配置一个简单的页面跳转 说明 OliverPro 为项目名称 ProApp 为应用程序 项目文件结构如下: 文件配置如下: 项目中的urls 应用程序urls views.py文件 ...
- Echarts实例
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- Ubuntu中类似任务管理器的东西?
Ubuntu里面有没有类似windows中任务管理器的东西呢?怎么打开?谢谢!!! ================================ 检举| 2009-02-01 16:50提问者 ...
- java web中使用log4j
测试log4j的项目结构 Log4j.properties的路径为 src/config/log4j Log4j.properties文件的内容 下面定义日志输出级别是 INFO,并且配置了2个 ...
- Linux防火墙的关闭和开启(转)
1) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重启后失效 开启: service iptables sta ...
- phpstorm设置断点调试
环境是:wamp PHP Version: 5.5.12 网上的教程很多,我自己按照教程操作,实现了断点调试,下面是我设置断点调试的步骤 1.修改配置文件php.ini,按下面修改(位置在最后) ; ...
- C# Debug
语法.IDE环境使用.Debug方法是学习一门语言的最少必须技能,本文总结C#中的最常用调试方法 一. 断点 如下图所示在欲插入断点的地方右键>断点>插入断点(或在行号左边点击)可在选中语 ...