我正在尝试为辅助技术目的创建一个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
}];
}
任何帮助实现这些目标之一?基本上修改NSEvent“keyboardEvent”以发送不同的字符。谢谢。
答案 0 :(得分:45)
您无法使用NSEvent
API执行此操作,但可以使用CGEventTap
执行此操作。您可以创建一个活动事件点按并注册接收CGEventRef
的{{3}}并可以修改它(如果需要)并返回它以修改实际的事件流。
编辑
这是一个简单的程序,在运行时,用“v”替换每个“b”键击:
#import <Cocoa/Cocoa.h>
CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
//0x0b is the virtual keycode for "b"
//0x09 is the virtual keycode for "v"
if (CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode) == 0x0B) {
CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, 0x09);
}
return event;
}
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
CFRunLoopSourceRef runLoopSource;
CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, kCGEventMaskForAllEvents, myCGEventCallback, NULL);
if (!eventTap) {
NSLog(@"Couldn't create event tap!");
exit(1);
}
runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
CFRunLoopRun();
CFRelease(eventTap);
CFRelease(runLoopSource);
[pool release];
exit(0);
}
(有趣的故事:当我正在编辑这篇文章时,我一直在尝试写“替换每个'b'键击”,但它仍然以“替换每个'v'键击”的形式出现。我很困惑。然后我记得我还没有停止过应用程序。)
答案 1 :(得分:4)
我遇到了这个答案,需要做同样的事情,但仅针对我自己的应用程序中的事件而不是全局事件。对于这个更简单的问题,有一个更简单的解决方案,我在这里注意到它对任何其他人都有用:
这对我来说似乎很有效,我甚至不必修改keyCode部分 - 但也许这可能是一个问题......
Swift中的示例:
class KeyInterceptorWindow : NSWindow {
override func sendEvent(theEvent: NSEvent) {
if theEvent.type == .KeyDown || theEvent.type == .KeyUp {
println(theEvent.description)
let newEvent = NSEvent.keyEventWithType(theEvent.type,
location: theEvent.locationInWindow,
modifierFlags: theEvent.modifierFlags,
timestamp: theEvent.timestamp,
windowNumber: theEvent.windowNumber,
context: theEvent.context,
characters: "H",
charactersIgnoringModifiers: theEvent.charactersIgnoringModifiers!,
isARepeat: theEvent.ARepeat,
keyCode: theEvent.keyCode)
super.sendEvent(newEvent!)
} else {
super.sendEvent(theEvent)
}
}
}
答案 2 :(得分:0)
james_alvarez's answer的4+版本:
class KeyInterceptorWindow: NSWindow {
override func sendEvent(_ event: NSEvent) {
if [.keyDown, .keyUp].contains(event.type) {
let newEvent = NSEvent.keyEvent(with: event.type,
location: event.locationInWindow,
modifierFlags: event.modifierFlags,
timestamp: event.timestamp,
windowNumber: event.windowNumber,
context: nil,
characters: "H",
charactersIgnoringModifiers: event.charactersIgnoringModifiers ?? "",
isARepeat: event.isARepeat,
keyCode: event.keyCode)
if let newEvent = newEvent {
super.sendEvent(newEvent)
}
} else {
super.sendEvent(event)
}
}
}