Question

I'm working on a project in visual c++ 2012 update 3. I need to hook keyboard for a little time and allow user to type in a password only. So i disable all keys except those required to type a password. It works well. But after unHooking the alt key presses automatically. i mean after unhooking, if i press tab key, it works as if i pressed alt + tab. In windows 8, win key pressed. But once i press the alt key manually on my keyboard, the problem is resolved. But this is a very serious case for me, because users surely loss trust in my application due to this strange behavior. Could any one please help.

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

if (nCode < 0 || nCode != HC_ACTION ) return CallNextHookEx( NULL, nCode, wParam, lParam); 

KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT*) lParam;

if(
    (p->vkCode == VK_BACK) ||
    (p->vkCode == VK_SHIFT) ||
    (p->vkCode == VK_CAPITAL) ||
    (p->vkCode == VK_SPACE) ||
    (p->vkCode == VK_HOME) ||
    (p->vkCode == VK_END) ||
    (p->vkCode == VK_LEFT) ||
    (p->vkCode == VK_RIGHT) ||
    (p->vkCode == VK_DELETE) ||
    (p->vkCode >= 0x30 && p->vkCode <= 0x39) ||
    (p->vkCode >= 0x41 && p->vkCode <= 0x5A) ||
    (p->vkCode >= 0x60 && p->vkCode <= 0x6F) ||
    (p->vkCode == 0x90) ||
    (p->vkCode == 0x91) ||
    (p->vkCode == 0xA0) ||
    (p->vkCode == 0xA1) ||
    (p->vkCode >= 0xBA && p->vkCode <= 0xC0) ||
    (p->vkCode >= 0xDB && p->vkCode <= 0xDF) ||
    (p->vkCode == 0xE2)
    )
    {
        return CallNextHookEx(NULL, nCode, wParam, lParam);  
    }
    else
        return 1;   
}
Was it helpful?

Solution

If the alt key is pressed before your application begins, or held down when your application cannot hook it (e.g., sent to a window of a process run as an administrator), your hook will eat the keyup for it and the rest of the system will still think it's being pressed down when the user stops pressing it.

You could use SendInput to send a keyup for it after your hook is done (or during with a filter for it built into your hook), but that won't ensure the window that got the keydown knows it's up. I'm not sure there is a particularly easy solution to that part.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top