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

时间:2009-08-02 14:45:35

标签: c# .net input idle-processing

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

  

GetLastInputInfo不提供   系统范围的用户输入信息   跨所有正在运行的会话相反,   GetLastInputInfo提供   特定于会话的用户输入   只有会话的信息   调用了这个函数。

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

1 个答案:

答案 0 :(得分:2)

我认为这样做的唯一方法是通过挂钩进入外壳。

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

随着Vista之间的会话隔离,您需要提升权限才能为其他会话执行此操作。在这个从键盘记录器的工作方式中获取线索可能会有所帮助,但我没有为此提供一些准备链接。

首先从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;
}