代码之家  ›  专栏  ›  技术社区  ›  Kamiel Wanrooij

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

  •  3
  • Kamiel Wanrooij  · 技术社区  · 6 年前

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

    GetLastInputInfo不提供 在所有正在运行的会话中。相当地 特定于会话的用户输入 仅适用于以下会话的信息: 调用该函数。

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

    1 回复  |  直到 14 年前
        1
  •  2
  •   ShuggyCoUk    15 年前

    我相信唯一的方法是通过钩住外壳。

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

    首先,这里是来自windows端口的源代码 condor 要查看键盘活动,请执行以下操作:

    /***************************************************************
     *
     * 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;
    }
    
    推荐文章