hookkey.cpp

来自「AVI这个软件是用VC编写的」· C++ 代码 · 共 105 行

CPP
105
字号
// MouseHook.cpp : Defines the entry point for the DLL application.
//


#include "stdafx.h"
#define _COMPILING_44E531B1_14D3_11d5_A025_006067718D00
#include "Hookkey.h"
#include <stdio.h>
#include <mmsystem.h>

#pragma data_seg(".SHAREKEY")
HWND hWndServerKey = NULL;
UINT nMsgKey = 0;
HHOOK hookKey = NULL;
unsigned long oldKeyTime = 0;
#pragma data_seg()
#pragma comment(linker, "/section:.SHAREKEY,rws")

HINSTANCE hInst;

UINT WM_USER_KEYSTART;

static LRESULT CALLBACK hookkeyproc(UINT nCode, WPARAM wParam, LPARAM lParam);

BOOL APIENTRY DllMain( HINSTANCE hInstance, 
                       DWORD  Reason, 
                       LPVOID Reserved
					 )
{
 switch(Reason)
    { /* reason */
     case DLL_PROCESS_ATTACH:
	hInst = hInstance;
			WM_USER_KEYSTART = RegisterWindowMessage(WM_USER_KEYSTART_MSG);
	return TRUE;
     case DLL_PROCESS_DETACH:
	if(hWndServerKey != NULL)
	   UninstallMyKeyHook(hWndServerKey);
	return TRUE;
    } /* reason */
    return TRUE;
}


__declspec(dllexport) BOOL InstallMyKeyHook(HWND hWnd, UINT message_to_call)
    {
     if(hWndServerKey != NULL)
	return FALSE; // already hooked!
     //hook = SetWindowsHookEx(WH_GETMESSAGE,
	 hookKey = SetWindowsHookEx(WH_KEYBOARD,
			    (HOOKPROC)hookkeyproc,
			    hInst,
			    0);
     if(hookKey != NULL)
	{ /* success */
	 hWndServerKey = hWnd;
	 nMsgKey = message_to_call;
	 
	 return TRUE;
	} /* success */
     return FALSE; // failed to set hook
    } // setMyHook


__declspec(dllexport) BOOL UninstallMyKeyHook(HWND hWnd)
    {
     if(hWnd != hWndServerKey || hWnd == NULL)
	return FALSE;
     BOOL unhooked = UnhookWindowsHookEx(hookKey);
     if(unhooked)
	hWndServerKey = NULL;
     return unhooked;
    } // clearMyHook



static LRESULT CALLBACK hookkeyproc(UINT nCode, WPARAM wParam, LPARAM lParam)
{
    if(nCode < 0)
	{ /* pass it on */
	 CallNextHookEx(hookKey, nCode, wParam, lParam);
	 return 0;
	} /* pass it on */
     

	
		//Can this code prevent too many messages from being sent to vscap	and cause it to crash ?		
		unsigned long currentKeyTime = timeGetTime();
		unsigned long difftime = currentKeyTime - oldKeyTime;
		if (difftime>100) {

			if (lParam>0) {
				PostMessage(hWndServerKey, WM_USER_KEYSTART , wParam, lParam);
				oldKeyTime = currentKeyTime;
			}

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

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?