c# - GetLastInputInfo 是用户特定的 - 是否有类似的东西可以提供机器范围的最后输入时间

标签 c# .net input idle-processing

根据 MSDN,http://msdn.microsoft.com/en-us/library/ms646302%28VS.85%29.aspx

GetLastInputInfo does not provide system-wide user input information across all running sessions. Rather, GetLastInputInfo provides session-specific user input information for only the session that invoked the function.

是否有类似的东西可以提供系统范围内的最后用户输入信息?

最佳答案

我相信做到这一点的唯一方法是 Hook 到 shell。

这是(显然)必须小心完成的事情,并且在操作系统完全支持之前(直到 windows 7)在托管代码中是不可行的,因此您将不得不使用一些非托管代码来实现这一点,也许从您的托管代码更新一些可查询的全局状态。 API 是 SetWindowsHookEx .

随着 Vista 的 session 隔离,您将需要更高的权限才能为其他 session 执行此操作。在这方面,从键盘记录器的工作方式中获取线索可能会有所帮助,但我没有现成的指向某些来源的链接。

这里的第一个开始是来自 condor 的 Windows 端口的源代码用于发现键盘事件:

/***************************************************************
 *
 * Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
 * University of Wisconsin-Madison, WI.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You may
 * obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************/


#include <windows.h>

// Shared DATA
// put in here data that is needed globally
#pragma data_seg(".SHARDATA")
HHOOK hHook = NULL;
LONG KBkeyhitflag = 0;
#pragma data_seg()
#pragma comment(linker, "/SECTION:.SHARDATA,RWS")

__declspec(dllexport) LRESULT CALLBACK KBHook(int nCode, WPARAM wParam,
LPARAM lParam)
{
    InterlockedExchange(&KBkeyhitflag,1);

    return CallNextHookEx(hHook,nCode,wParam,lParam);
}

HINSTANCE g_hinstDLL = NULL;

#if defined(__cplusplus)
extern "C" {
#endif //__cplusplus


int __declspec( dllexport) WINAPI KBInitialize(void)
{
    hHook=(HHOOK)SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KBHook,g_hinstDLL,0);
    return hHook ? 1 : 0;
}

int __declspec( dllexport) WINAPI KBShutdown(void)
{
    if ( UnhookWindowsHookEx(hHook) )
        return 1;   // success
    else
        return 0;   // failure
}

int __declspec( dllexport)  WINAPI KBQuery(void)
{
    if ( InterlockedExchange(&KBkeyhitflag,0) )
        return 1;   // a key has been hit since last query
    else
        return 0;   // no keys hit since asked last
}

#if defined(__cplusplus)
} // extern "C"
#endif //defined(__cplusplus)

BOOL WINAPI DllMain(HANDLE hInstDLL, ULONG fdwReason, LPVOID lpReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            g_hinstDLL = (HINSTANCE)hInstDLL;
            DisableThreadLibraryCalls(g_hinstDLL);
            break;
    }
    return 1;
}

关于c# - GetLastInputInfo 是用户特定的 - 是否有类似的东西可以提供机器范围的最后输入时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1219050/

相关文章:

.net - .NET MailMessage 类注入(inject)安全吗?

c# - 移位运算符 >> 或 << 何时有用?

c - 如何在C89中管理终端unix中的unicode输入

c# - 使用 SignalR hub 向特定连接的用户广播消息

c# - Amazon EC2 开发堆栈

c# - 如何在 C# 中将固定字节/char[100] 转换为托管 char[]?

c# - 使用 BackgroundWorker 时未捕获异常

c# - 我应该如何组织两个对象以便能够将它们加入一个键?

c++ - 在进行终端输入时闪烁匹配括号,就像 CLISP 那样?

javascript - 如何避免用户在 WebView 中的表单字段中输入?