4

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

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

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

4

1 回答 1

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;
}
于 2009-08-02T21:43:33.390 回答