-1

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;   
}
Jasir
  • 677
  • 1
  • 8
  • 26
  • Is alt being held when the program starts? – chris Oct 26 '13 at 16:25
  • My program closes soon after user inserts a password. alt key is held after my program closes – Jasir Oct 26 '13 at 16:29
  • 2
    My point is that if a user is holding the alt key when your program starts, the keyup is eaten by your hook, and after your program closes, there's still no keyup, so the system thinks it's down. – chris Oct 26 '13 at 16:30
  • Got it!! Let me test it and will reply you soon.. Thanks – Jasir Oct 26 '13 at 16:34
  • Well, to be honest, I had the same issue in my "lock screen replacement" (for my own needs, just a message and the inability to do anything until a password is entered), which was started with ctrl-alt-L, so sometimes ctrl or alt would be stuck afterward. The difference is that I was the only one using it, so I didn't bother fixing that. – chris Oct 26 '13 at 16:39
  • Exactly thatz my problem. now i know why it happens. When user press alt+ctrl+del, which cannot be hooked, alt and ctrl becomes down state. It persists even after user leaves the ctrl+alt+del window. Great thanks.. If you posted it as an answer, i could have accepted it and you scored more.. :D – Jasir Oct 26 '13 at 16:50
  • Hmm, why would anybody want to activate a keyboard hook after the user presses Ctrl+Alt+Del? I can foresee the next question. – Hans Passant Oct 26 '13 at 17:36
  • Sorry, i didn't get you! – Jasir Oct 26 '13 at 18:20

1 Answers1

1

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.

chris
  • 60,560
  • 13
  • 143
  • 205