Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.
// HookDll.cpp
//
// This is the DLL which sets a hook which
// substitutes the letter 'y' when the user types
// the letter 'x'
//
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
HINSTANCE hMod;
HHOOK hHook;
bool g_ignoreChar;
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
hMod = (HINSTANCE) hModule;
g_ignoreChar = false;
return TRUE;
}
#ifdef __cplusplus
extern "C" {
#endif
LRESULT CALLBACK HookProc(int code, WPARAM wParam, LPARAM lParam)
{
LRESULT ret = 0;
if (code == HC_ACTION)
{
MSG * pMsg = (MSG *) lParam;
switch( pMsg->message )
{
case WM_KEYDOWN:
if (pMsg->wParam == 'Z')
{
g_ignoreChar = true;
pMsg->wParam = 'x';
pMsg->message = WM_CHAR;
CallNextHookEx(hHook, code, wParam, lParam);
pMsg->wParam = 'y';
return CallNextHookEx(hHook, code, wParam, lParam);
}
break;
case WM_KEYUP:
if (pMsg->wParam == 'Z')
return 0;
else
break;
case WM_CHAR:
if ( g_ignoreChar == true)
{
g_ignoreChar = false;
return 0;
}
else
break;
}
}
return(CallNextHookEx(hHook, code, wParam, lParam));
}
__declspec(dllexport) BOOL SetHook(DWORD threadId)
{
hHook = SetWindowsHookEx(WH_GETMESSAGE, HookProc, hMod, threadId);
return(hHook ? TRUE : FALSE);
}
__declspec(dllexport) void ClearHook(void)
{
if (hHook)
{
UnhookWindowsHookEx(hHook);
hHook = 0;
}
}
#ifdef __cplusplus
}
#endif
LRESULT CALLBACK HookProc(int code, WPARAM wParam, LPARAM lParam)
{
LRESULT ret = 0;
if (code == HC_ACTION)
{
MSG * pMsg = (MSG *) lParam;
switch( pMsg->message )
{
case WM_CHAR:
if ( pMsg->wParam == 'Z' )
{
pMsg->wParam = 'x';
CallNextHookEx(hHook, code, wParam, lParam);
if ( wParam == 0 )
::PostMessage( pMsg->hwnd, WM_CHAR, 'y', pMsg->lParam );
return TRUE;
}
break;
}
}
return(CallNextHookEx(hHook, code, wParam, lParam));
}
LRESULT CALLBACK HookProc(int code, WPARAM wParam, LPARAM lParam)
{
LRESULT ret = 0;
if (code == HC_ACTION)
{
MSG * pMsg = (MSG *) lParam;
switch( pMsg->message )
{
case WM_KEYDOWN:
if (pMsg->wParam == 'Z')
{
g_ignoreChar = true;
::SendMessage( pMsg->hwnd, WM_CHAR, 'y', pMsg->lParam );
pMsg->wParam = 'x';
pMsg->message = WM_CHAR;
}
break;
case WM_KEYUP:
if (pMsg->wParam == 'Z')
return 0;
else
break;
case WM_CHAR:
if ( g_ignoreChar == true)
{
g_ignoreChar = false;
return 0;
}
else
break;
}
}
return(CallNextHookEx(hHook, code, wParam, lParam));
}
LRESULT CALLBACK HookProc(int code, WPARAM wParam, LPARAM lParam)
{
static bool bEnabled = true;
if (code == HC_ACTION)
{
MSG * pMsg = (MSG *) lParam;
switch( pMsg->message )
{
case WM_KEYDOWN:
if (pMsg->wParam == 'Z')
{
pMsg->message = WM_NULL;
if (( pMsg->lParam & 0xFFFF ) == 1 && bEnabled )
{
::PostMessage( pMsg->hwnd, WM_CHAR, 'x', pMsg->lParam );
::PostMessage( pMsg->hwnd, WM_CHAR, 'y', pMsg->lParam );
bEnabled = false;
}
}
break;
case WM_KEYUP:
if (pMsg->wParam == 'Z')
{
bEnabled = true;
pMsg->message = WM_NULL;
}
}
}
return(CallNextHookEx(hHook, code, wParam, lParam));
}
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
case WM_CHAR:
if (pMsg->wParam == 'Z')
{
pMsg->wParam = 'x';
CallNextHookEx(hHook, code, wParam, lParam);
pMsg->wParam = 'y';
return CallNextHookEx(hHook, code, wParam, lParam);