Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Input.h"
- CInput* g_pInput = new CInput();
- CInput::CInput() {
- }
- CInput::~CInput() {
- RemoveHook();
- }
- HWND hGameWnd = NULL;
- BOOL WINAPI EnumWnd(HWND hWnd, LPARAM lParam) {
- DWORD dwProcessId = NULL;
- GetWindowThreadProcessId(hWnd, &dwProcessId);
- if (dwProcessId == lParam) {
- hGameWnd = hWnd;
- return FALSE;
- }
- return TRUE;
- }
- void CInput::SetupHook() {
- EnumWindows(EnumWnd, GetCurrentProcessId());
- if (!(m_WndOriginal = (WndProcFn)SetWindowLongPtr(hGameWnd, -4, (LONG_PTR)this->WndHook))) {
- DbgPrint(PREFIX"Failed to install keyboard hook!");
- }
- }
- void CInput::RemoveHook() {
- SetWindowLongPtr(hGameWnd, -4, (LONG_PTR)this->m_WndOriginal);
- }
- LRESULT WINAPI CInput::WndHook(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
- BOOL bUpdate = FALSE;
- BOOL bIsDown = FALSE;
- if (message == WM_KEYDOWN) {
- bUpdate = TRUE;
- bIsDown = TRUE;
- } else if (message == WM_KEYUP) {
- bUpdate = TRUE;
- bIsDown = FALSE;
- }
- WORD wVk = LOWORD(wParam);
- g_pInput->UpdateAsyncKeyState(wVk, bIsDown);
- return g_pInput->m_WndOriginal(hWnd, message, wParam, lParam);
- }
- WORD CInput::GetKeyState(int Key) {
- WORD wRet = 0;
- if (Key >= 0x100) {
- return 0;
- }
- if (IS_KEY_DOWN(m_AsyncKeyState, Key)) {
- wRet |= 0x8000;
- }
- if (m_AsyncKeyStateRecentDown[Key / 8] & (1 << (Key % 8))) {
- wRet |= 0x1;
- }
- m_AsyncKeyStateRecentDown[Key / 8] &= ~(1 << (Key % 8));
- return wRet;
- }
- void CInput::UpdateAsyncKeyState(WORD wVk, BOOL bIsDown) {
- if (bIsDown) {
- if (!IS_KEY_DOWN(m_AsyncKeyState, wVk)) {
- SET_KEY_LOCKED(m_AsyncKeyState, wVk, !IS_KEY_LOCKED(m_AsyncKeyState, wVk));
- }
- SET_KEY_DOWN(m_AsyncKeyState, wVk, TRUE);
- m_AsyncKeyStateRecentDown[wVk / 8] |= (1 << (wVk % 8));
- } else {
- SET_KEY_DOWN(m_AsyncKeyState, wVk, FALSE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement