在 cmets 中仍然没有收到您的答案,但因为这可能对其未来的其他人也有帮助,所以我还是决定回答。
使用 IOKit,您可以检测键盘是否有设备,并像设备事件一样获取按键事件。我用它来检测操纵杆事件,但它应该可以很好地与键盘一起使用。我认为我所做的修改已经足够并且应该可以工作,但是我的 Xcode 现在正在更新,所以我还不能测试它。
KeyboardWatcher.h 文件:
#import <Foundation/Foundation.h>
#import <IOKit/hid/IOHIDManager.h>
#import <IOKit/hid/IOHIDKeys.h>
@interface KeyboardWatcher : NSObject{
IOHIDManagerRef HIDManager;
}
@property (nonatomic) int keysPressedCount;
+(instancetype)sharedWatcher;
-(void)startWatching;
-(void)stopWatching;
@end
KeyboardWatcher.m 文件:
#import "KeyboardWatcher.h"
@implementation KeyboardWatcher
static KeyboardWatcher *_sharedWatcher;
+(instancetype)sharedWatcher {
@synchronized([self class]) {
if (!_sharedWatcher){
_sharedWatcher = [[KeyboardWatcher alloc] init];
}
return _sharedWatcher;
}
return nil;
}
-(instancetype)init {
self = [super init];
if (self){
self.keysPressedCount = 0;
}
return self;
}
-(void)startWatching {
[self watchDevicesOfType:kHIDUsage_GD_Keyboard];
}
-(void)watchDevicesOfType:(UInt32)deviceType {
// Create an HID Manager
HIDManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
// Create a Matching Dictionary
CFMutableDictionaryRef matchDict = CFDictionaryCreateMutable(kCFAllocatorDefault, 2, &kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
// That will make the app just return the computer keyboards
CFDictionarySetValue(matchDict, CFSTR(kIOHIDPrimaryUsageKey), CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &deviceType));
// Register the Matching Dictionary to the HID Manager
IOHIDManagerSetDeviceMatching(HIDManager, matchDict);
// Register the HID Manager on our app’s run loop
IOHIDManagerScheduleWithRunLoop(HIDManager, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
// Open the HID Manager
IOReturn IOReturn = IOHIDManagerOpen(HIDManager, kIOHIDOptionsTypeNone);
// Register input calls to Handle_DeviceEventCallback function
IOHIDManagerRegisterInputValueCallback(HIDManager, Handle_DeviceEventCallback, nil);
if (IOReturn) NSLog(@"IOHIDManagerOpen failed.");
}
-(void)stopWatching {
HIDManager = NULL;
}
static void Handle_DeviceEventCallback (void *inContext, IOReturn inResult, void *inSender, IOHIDValueRef value){
IOHIDElementRef element = IOHIDValueGetElement(value); // Keyboard pressed key
uint32_t uniqueIdentifier = IOHIDElementGetCookie(element); // Unique ID of key
int elementValue = (int)IOHIDValueGetIntegerValue(value); // Actual state of key (1=pressed)
NSLog(@"Unique ID = %u; Value = %d", uniqueIdentifier, elementValue);
if (elementValue == 1) KeyboardWatcher.sharedWatcher.keysPressedCount++;
}
@end
如果您想确定哪个唯一 ID 是哪个键,您可以使用这些枚举(而不是导入 Carbon,您可以创建一个 CGKeyboardMapping.h 文件并将它们粘贴到那里):
https://stackoverflow.com/a/16125341/4370893
最后,为了使用它,你只需要开始监听键盘事件:
[[KeyboardWatcher sharedWatcher] startWatching];
获取按键次数:
[[KeyboardWatcher sharedWatcher] keysPressedCount];
然后停止:
[[KeyboardWatcher sharedWatcher] stopWatching];
这些是我编写原始操纵杆代码的参考资料:
一旦更新完成,我将测试代码并确定它是否正常工作。
编辑:刚刚经过测试,它正在工作。不要忘记将 IOKit 框架添加到项目中。