gpt4 book ai didi

c++ - 将 Objective-C 代码转换为 C++ 以检测 OS X 上的用户空闲时间

转载 作者:太空狗 更新时间:2023-10-29 21:30:02 24 4
gpt4 key购买 nike

我正在开发 Qt/C++ 应用程序,我需要一个简单的函数来检索我在 Mac OS X 上以秒为单位的用户空闲时间。

我找到了这段检测用户空闲时间的代码。

#include <IOKit/IOKitLib.h>

/**
Returns the number of seconds the machine has been idle or -1 if an error occurs.
The code is compatible with Tiger/10.4 and later (but not iOS).
*/
int64_t SystemIdleTime(void) {
int64_t idlesecs = -1;
io_iterator_t iter = 0;
if (IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching("IOHIDSystem"), &iter) == KERN_SUCCESS) {
io_registry_entry_t entry = IOIteratorNext(iter);
if (entry) {
CFMutableDictionaryRef dict = NULL;
if (IORegistryEntryCreateCFProperties(entry, &dict, kCFAllocatorDefault, 0) == KERN_SUCCESS) {
CFNumberRef obj = CFDictionaryGetValue(dict, CFSTR("HIDIdleTime"));
if (obj) {
int64_t nanoseconds = 0;
if (CFNumberGetValue(obj, kCFNumberSInt64Type, &nanoseconds)) {
idlesecs = (nanoseconds >> 30); // Divide by 10^9 to convert from nanoseconds to seconds.
}
}
CFRelease(dict);
}
IOObjectRelease(entry);
}
IOObjectRelease(iter);
}
return idlesecs;
}

如何将此代码转换为 C++,以用于我的 Qt/C++ 项目?

最佳答案

您只需要在链接框架列表中添加IOKit.framework。将框架视为一组共享库和相关资源。 IOKit.framework 位于

 /System/Library/Frameworks/IOKit.framework

我不知道如何在 Qt 项目中做到这一点;该项目应该有一个你想要链接的额外框架列表。如果它是一个标准的 XCode 项目,则有一个名为 add a framework to the project 或类似名称的菜单项。

关于c++ - 将 Objective-C 代码转换为 C++ 以检测 OS X 上的用户空闲时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3547601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com