Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

objective c - OS X count pressed keys without accessibility access

I found that upwork.app can count pressed keys without accessibility access but I can not understand how it is done.

I read a lot of themes like this one: OSX: Detect system-wide keyDown events?

In all themes are saying that process should be trusted "Enable access for assistive devices" and I can not find how upwork.app can track keys without this.

This is official documentation for tracking events:
https://developer.apple.com/reference/appkit/nsevent/1535472-addglobalmonitorforeventsmatchin

Key-related events may only be monitored if accessibility is enabled or if your application is trusted for accessibility access (see AXIsProcessTrusted).

In apple mail list is saying same:
http://lists.apple.com/archives/carbon-dev/2010/Feb/msg00043.html

I think that upwork.app use some hacks.

How I can count pressed keys without accessibility access?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Still haven't received your answer in the comments, but since that might also help other people in the future I decided to answer anyway.

With the IOKit you can detect the keyboard has a device, and get the key press events like device events. I've used that to detect joystick events, but it should work with keyboards has well. I assume that the modifications that I made are enough and should work, however my Xcode is updating now, so I wasn't able to test it yet.

KeyboardWatcher.h File:

#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 File:

#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

In case you want to identify which unique ID is which key, you can use these enums (instead of importing Carbon you can just create a CGKeyboardMapping.h file and paste them there): https://stackoverflow.com/a/16125341/4370893

At last, in order to use it, you just need to do that to start watching for keyboard events:

[[KeyboardWatcher sharedWatcher] startWatching];

Get the key pressed count with that:

[[KeyboardWatcher sharedWatcher] keysPressedCount];

And that to stop:

[[KeyboardWatcher sharedWatcher] stopWatching];

These were my references to write my original joystick code:

As soon as the update finishes I will test the code and inform if it's working or not for sure.

EDIT: Just tested and it's working. Don't forget to add the IOKit framework to the project.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...