我正在尝试创建一个OS X键盘钩子用于辅助技术目的(即不要担心,而不是键盘记录器).
当用户按下某个键时,我想阻止真正的按键并发送假按键(我选择的字符).
我有以下代码:
- (void) hookTheKeyboard {
CGEventMask keyboardMask = CGEventMaskBit(kCGEventKeyDown);
id eventHandler = [NSEvent addGlobalMonitorForEventsMatchingMask:keyboardMask handler:^(NSEvent *keyboardEvent) {
NSLog(@"keyDown: %c", [[keyboardEvent characters] characterAtIndex:0]);
//Want to: Stop the keyboard input
//Want to: Send another key input instead
}];
}
Run Code Online (Sandbox Code Playgroud)
有任何帮助实现这些目标吗?基本上修改NSEvent"keyboardEvent"以发送不同的字符.谢谢.
我正在尝试添加一个事件陷阱来启用/禁用魔术触控板上的事件.我认为这将是直截了当的,即注册事件陷阱,并在需要时通过返回丢弃事件NULL.我们的想法是使用pad进行一些特定的,耗时的数据输入,输入数据的应用程序是第三方的,所以我不能只是添加代码来实现我想要的.所以我想我会监视系统事件,然后通过一堆CGEventCreateKeyboardEvents 发送所需的输入.
问题是返回null似乎没有丢弃事件,更多的调查表明,这不仅限于来自触控板的那些,而且也是我的默认USB鼠标.
我的代码如下.以下是我希望不能移动鼠标,如果我改变(A)使用kCGEventScrollWheel或kCGEventLeftMouseDragged然后消耗事件,即滚动或左btn拖动不会发生.这是否意味着并非所有事件都可以丢弃?希望我在这里错过了一些明显的东西
#define case_print(a) case a: printf("%s - %d\n",#a,a); break;
CGEventRef eventOccurred(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void* refcon) {
int subType = CGEventGetIntegerValueField(event, kCGMouseEventSubtype);
if (type == NSEventTypeGesture || subType == NX_SUBTYPE_MOUSE_TOUCH) {
printf("touchpad\n");
switch(type) {
case_print(kCGEventNull)
case_print(kCGEventLeftMouseDown)
case_print(kCGEventLeftMouseUp)
case_print(kCGEventRightMouseDown)
case_print(kCGEventRightMouseUp)
case_print(kCGEventMouseMoved)
case_print(kCGEventLeftMouseDragged)
case_print(kCGEventRightMouseDragged)
case_print(kCGEventScrollWheel)
case_print(kCGEventOtherMouseDown)
case_print(kCGEventOtherMouseUp)
case_print(kCGEventOtherMouseDragged)
case_print(kCGEventTapDisabledByTimeout)
case_print(kCGEventTapDisabledByUserInput)
case_print(NSEventTypeGesture)
case_print(NSEventTypeMagnify)
case_print(NSEventTypeSwipe)
case_print(NSEventTypeRotate)
case_print(NSEventTypeBeginGesture)
case_print(NSEventTypeEndGesture)
default:
printf("default: %d\n",type);
break;
}
event = NULL;
} else {
if (type …Run Code Online (Sandbox Code Playgroud)