Quantcast
Channel: Project Customization and Programming forum
Viewing all articles
Browse latest Browse all 5347

Keyboard hook

$
0
0
Hello,
First of all, excuse my languange, since English is no my native lang.
I try to hook the keyboard with SetWindowsHookEx() func, but something strange happened, please take a look at this code:
...
HINSTANCE hInst;
...
HHOOK g_hKbHook;  // Hook handle
HWND g_hEdit;     // Handle to Edit controlwchar_t *g_pBuff;   // pointer to buffer in global
...void ToggleKbHook(HWND hWnd)
{
...
g_hKbHook = ::SetWindowsHookEx(WH_KEYBOARD, reinterpret_cast<HOOKPROC>(KeybHookProc), hInst, ::GetCurrentThreadId());
...
}
...
LRESULT CALLBACK KeybHookProc(int nCode, WPARAM wParm, LPARAM lParm)
{
	UINT mapRes = 0;switch (static_cast<unsignedint>(wParm))
	{case VK_SHIFT:
		::lstrcat(g_pBuff, L"[SHIFT]");break;case VK_RETURN:
		::lstrcat(g_pBuff, L"[ENTER]");break;case VK_TAB:
		::lstrcat(g_pBuff, L"[TAB]");break;	case VK_ESCAPE:
		::lstrcat(g_pBuff, L"[ESC]");break;case VK_CAPITAL:
		::lstrcat(g_pBuff, L"[CAPS]");break;default:
		mapRes = ::MapVirtualKey(static_cast<UINT>(wParm), MAPVK_VK_TO_CHAR);if (mapRes > 0){wchar_t res[2];
			res[0] = static_cast<wchar_t>(mapRes);
			res[1] = L'\0';
			::lstrcat(g_pBuff, &res[0]);
		} elseif (mapRes == 0) {
			::lstrcat(g_pBuff, L"[UNKNOWN_KEY]");
		} else {

		}
		break;
	}if (g_hEdit){if (::lstrlen(g_pBuff) > 0){
			::SetWindowText(g_hEdit, reinterpret_cast<LPWSTR>(g_pBuff));
		}
	}return ::CallNextHookEx(g_hKbHook, nCode, wParm, lParm);
}
When I put one or more breakpoint inside function KeybHokkProc(), the programm running as I expected, Edit control display text properly, but when I remove the breakpoints Edit control show "double characters", it mean when I press character A in keyboard, the Edit control show double character A (AA).
Why is this happen ?
Thanks before

Viewing all articles
Browse latest Browse all 5347

Trending Articles