Disable ALT key commands

This is a discussion on Disable ALT key commands within the Windows Programming forums, part of the Platform Specific Boards category; I'm using Charles Petzold's Programming Windows books and he suggests adding this code to your windows procedure to disable the ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    207

    Disable ALT key commands

    I'm using Charles Petzold's Programming Windows books and he suggests adding this code to your windows procedure to disable the ALT key commands:

    Code:
    case WM_SYSKEYDOWN:
    case WM_SYSKEYUP:
    case WM_SYSCHAR:
            return (0);
    I added the code and tried it on Windows XP and it has no effect!? I also tried it on Windows 98 and it has no effect there either?! In each case I made certain that my program had the focus (and I even closed all other windows to make sure).

    Does anyone know how to disable the ALT key commands (CTRL-ALT-DEL, ALT-TAB, etc) using the Win API?

    Someone told me that the ALT key commands are at a lower level than the Win API in Windows XP, so there was no effective way to disable them. However, I would think that you'd at least be able to disable them in Windows 98!

    Also, what's the point in having a Win API if it prevents you from doing certain things? This isn't Bill's Windows, this is MY Windows!

    mw
    Blucast Corporation

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    207
    Wow...

    That is just too beautiful for words.

    Thank you.

    mw
    Blucast Corporation

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    207
    Ok, my smart aleck partner found a way to get around disabled ALT keys by using the Windows key.

    Is there any way to disable ALL keys except for:

    letters
    number keys (and/or number pad)
    space bar
    enter key
    shift keys
    tab key
    cursor keys

    mw
    Blucast Corporation

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Out of curiousity, why do you need to do all of this?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    207
    We're creating a game and I wanted to prevent the player from leaving the window during gameplay. I've seen this in games I've played before but I didn't realize I would have to hack the darn computer to get it to work.

    Do you know how to disable all keys except the ones I listed?

    mw
    Blucast Corporation

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    > I wanted to prevent the player from leaving the window during gameplay
    How presumptious of you!.
    Like it's your right to take away control of my machine from me.
    Games like that find their way into the bit bucket in double-quick time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    207
    So I take it you don't know how to do this?

    Quote Originally Posted by Lionmane
    Is there any way to disable ALL keys except for:

    letters
    number keys (and/or number pad)
    space bar
    enter key
    shift keys
    tab key
    cursor keys
    mw

    PS: I'll forward your input to my boss.
    Last edited by Lionmane; 09-22-2005 at 09:28 AM.
    Blucast Corporation

  9. #9
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Ok I just ripped this straight from some of my code .. it should help get you going. Preventing users from switching is sort of frown upon, however there are times when it is needed and the programmer should use at their own discretion. In my case this is from a child's game (my 2 year old) and I absolutely didn't want him exiting out, however there was a convient way to exit that couldn't be done accidently built in.

    Code:
    //set keyboardhook
        hKeyboardHook = SetWindowsHookEx( WH_KEYBOARD_LL,  LowLevelKeyboardProc, GetModuleHandle(NULL), 0 );
    
    ...
    
    // keyboard hook proc
    LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam )
    {
        if (nCode < 0 || nCode != HC_ACTION ) return CallNextHookEx( hKeyboardHook, nCode, wParam, lParam); 
    	
    	KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT*)lParam;
    
        if(p->vkCode==VK_TAB && p->flags & LLKHF_ALTDOWN) return 1; //disable alt-tab
    	if((p->vkCode == VK_LWIN) || (p->vkCode == VK_RWIN)) return 1;//disable windows keys
    	if (p->vkCode == VK_ESCAPE && p->flags & LLKHF_ALTDOWN) return 1;//disable alt-escape
    	BOOL bControlKeyDown = GetAsyncKeyState (VK_CONTROL) >> ((sizeof(SHORT) * 8) - 1);//checks ctrl key pressed
    	if (p->vkCode == VK_ESCAPE && bControlKeyDown) return 1; //disable ctrl-escape
    	
    	return CallNextHookEx( hKeyboardHook, nCode, wParam, lParam );
    }
    
    ...
    // cleanup
        UnhookWindowsHookEx( hKeyboardHook );
    You return 1 (TRUE) from the keyboardhook proc for any key press/combination you want disabled or CallNestHookEx() for the ones you want to allow
    Last edited by Darryl; 09-22-2005 at 01:13 PM.

  10. #10
    Registered User
    Join Date
    May 2005
    Posts
    207
    Ok, thanks!

    mw
    Blucast Corporation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21