【问题标题】:Replacement for deprecated NXOpenEventStatus?替换已弃用的 NXOpenEventStatus?
【发布时间】:2021-03-06 06:46:22
【问题描述】:

我需要在 OSX 10.13 上获取鼠标的跟踪速度。我在互联网上找到了这段代码,但 NXOpenEventStatus 已被弃用(就像 IOHIDGetAccelerationWithKey 一样),是否有替代方法?

#include <stdio.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/hidsystem/IOHIDLib.h>
#include <IOKit/hidsystem/IOHIDParameter.h>
#include <IOKit/hidsystem/event_status_driver.h>

int main()
{
    kern_return_t kr;
    double trackpadAcceleration, mouseAcceleration;
    NXEventHandle h = 0;

    h = NXOpenEventStatus();

    if (h == nil)
        return -1;


   kr = IOHIDGetAccelerationWithKey( h, CFSTR(kIOHIDMouseAccelerationType), &mouseAcceleration);


   return 0;
}

【问题讨论】:

    标签: objective-c macos cocoa


    【解决方案1】:

    由于NXOpenEventStatusIOHIDGetAccelerationWithKey 都是开源IOKit 发行版的一部分,您可以look at how they're implemented。事实证明,我们可以做那些函数所做的事情,只使用非弃用函数。

    要将其归结为最低限度,您可以获得 HID 系统属性的字典,如下所示:

    #import <Foundation/Foundation.h>
    #import <IOKit/hidsystem/IOHIDLib.h>
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            io_service_t service = IORegistryEntryFromPath(kIOMasterPortDefault, kIOServicePlane ":/IOResources/IOHIDSystem");
    
            CFDictionaryRef parameters = IORegistryEntryCreateCFProperty(service, CFSTR(kIOHIDParametersKey), kCFAllocatorDefault, kNilOptions);
            NSLog(@"%@", parameters);
    
            IOObjectRelease(service);
        }
        return 0;
    }
    

    输出(对于我在 macOS 10.13.4 上):

    2018-04-05 17:06:55.560590-0500 accel[11924:131983] {
        ActuateDetents = 1;
        Clicking = 0;
        DragLock = 0;
        Dragging = 0;
        EjectDelay = 0;
        FirstClickThreshold = 1;
        ForceSuppressed = 0;
        HIDClickSpace =     (
            5,
            5
        );
        HIDClickTime = 500000000;
        HIDDefaultParameters = 1;
        HIDF12EjectDelay = 250;
        HIDFKeyMode = 1;
        HIDInitialKeyRepeat = 250000000;
        HIDKeyRepeat = 33333333;
        HIDKeyboardModifierMappingPairs =     (
        );
        HIDMouseAcceleration = 98304;
        HIDMouseKeysOptionToggles = 0;
        HIDPointerAcceleration = 45056;
        HIDPointerButtonMode = 2;
        HIDScrollAcceleration = 20480;
        HIDScrollZoomModifierMask = 262144;
        HIDSlowKeysDelay = 0;
        HIDStickyKeysDisabled = 0;
        HIDStickyKeysOn = 0;
        HIDStickyKeysShiftToggles = 0;
        HIDTrackpadAcceleration = 57344;
        HIDWaitCursorFrameInterval = 16666667;
        JitterNoClick = 1;
        JitterNoMove = 1;
        MouseButtonDivision = 55;
        MouseButtonMode = TwoButton;
        MouseHorizontalScroll = 1;
        MouseMomentumScroll = 1;
        MouseOneFingerDoubleTapGesture = 0;
        MouseTwoFingerDoubleTapGesture = 0;
        MouseTwoFingerHorizSwipeGesture = 0;
        MouseVerticalScroll = 1;
        "OutsidezoneNoAction When Typing" = 1;
        "PalmNoAction Permanent" = 1;
        "PalmNoAction When Typing" = 1;
        SecondClickThreshold = 1;
        "Trackpad Jitter Milliseconds" = 192;
        TrackpadCornerSecondaryClick = 0;
        TrackpadFiveFingerPinchGesture = 0;
        TrackpadFourFingerHorizSwipeGesture = 2;
        TrackpadFourFingerPinchGesture = 0;
        TrackpadFourFingerVertSwipeGesture = 0;
        TrackpadHandResting = 1;
        TrackpadHorizScroll = 1;
        TrackpadMomentumScroll = 1;
        TrackpadPinch = 1;
        TrackpadRightClick = 1;
        TrackpadRotate = 1;
        TrackpadScroll = 1;
        TrackpadThreeFingerDrag = 0;
        TrackpadThreeFingerHorizSwipeGesture = 2;
        TrackpadThreeFingerTapGesture = 0;
        TrackpadThreeFingerVertSwipeGesture = 0;
        TrackpadThreeFingersRightClick = 0;
        TrackpadTwoFingerDoubleTapGesture = 1;
        TrackpadTwoFingerFromRightEdgeSwipeGesture = 0;
        TwofingerNoAction = 1;
        USBMouseStopsTrackpad = 0;
        "Use Panther Settings for W" = 0;
        UserPreferences = 1;
        version = 1;
    }
    Program ended with exit code: 0
    

    kIOHIDMouseAccelerationType 常量的值为 HIDMouseAcceleration。我还在那里看到HIDPointerAccelerationHIDTrackpadAcceleration。也有 kIOHID... 常量。

    还要注意IOHIDGetAccelerationWithKey 在返回之前将注册表值除以 65536。 IOHIDSetAccelerationWithKey 执行相反的转换。

    【讨论】:

    • 很好的答案,我不知道 IOKit 是开源的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 2014-02-19
    • 2019-12-20
    • 2018-06-04
    相关资源
    最近更新 更多