• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1443
  • Last Modified:

How to Lock "Alt + Tab" key

Hello experts
I want to lock Alt + Tab key how can i do that using registry edit

  • 10
  • 5
  • 3
  • +6
1 Solution
HKEY_CURRENT_USER\Control Panel\Desktop

Key  Coolswitch
1 enable switching
0 disable switching

You may have to restart the machine
crystalsoftAuthor Commented:

Thanks Experts

milindsm:   i followed your suggestion but its Not getting what i want (Lock "Alt + Tab")

K4U:   can you Please provide me following code in vb6 (I get this from your suggested link)
  WPARAM wParam, LPARAM lParam)
  if (uMsg == WM_HOTKEY)
     // Ctrl+Alt+Del

  return 1;
  return CallWindowProc(OldSASWindowProc, hWnd, wParam, lParam);

Is there any other way to lock alt+tab (like Registry edit)

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Daniel Van Der WerkenIndependent ConsultantCommented:
Looks like the registry setting for this is documented here:
Of note:

HKCU HKEY_CURRENT_USER\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System


Setting:  One of the following:
      TASKMGR  = 0x01,  // disable task manager (Ctrl+Alt+Del)
      TASKKEYS = 0x02,  // disable task keys (Alt-TAB, etc)
      TASKBAR  = 0x04,  // disable task bar
      ALL=0xFFFF        // disable everything :(
I don't have access to my local system to test (not admin so I can't create the System key).  See if setting the DWORD value to 0x00000002 works.
crystalsoftAuthor Commented:


Case 1
    nDrives = 2
    wshShell.regwrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System\DisableTaskMgr", CLng(nDrives), "REG_DWORD"

Not working
Daniel Van Der WerkenIndependent ConsultantCommented:
Did you reboot or restart the shell?

Reboot with the setting.  See if it works.

crystalsoftAuthor Commented:
No i didnt restarted , But i am using following code instead of restart system
 Dim bogus As Long
  Call SendMessageTimeoutA( _
    0, _
    0, _
    1000, _

Actually i don't want to restart system,
Chris Raisin(Retired Analyst/Programmer)Commented:
When you say "Lock" are you meaning "Disable", so that the user cannot press the CAPS key (you want all input in lower case) or the TAB key (you want the user only to be able to press the spacebar to put spaces in any input).
Or do you perhaps want the CAPS locked ON? (All input is in capitals).
Is this input ONLY for within your program, or do you want it to affect every program running on a machine?
If the answer to the last question is "Yes", then writing to the registry is what is required.
Of course, this is in effect unsound programming since it is doing what "virus"s  often do!

If the changes are only effective within your program (which is what you should be doing, not changing external conditions outside a program without the user knowing it) then that can be done using VB code internal to your program, without rebooting, and I have written some code that does that.

Please advise.
Chris Raisin(Retired Analyst/Programmer)Commented:
Or do you mean "Alt+Tab" together which brings up the "Program switching" functionality external to any program (I gather that is what you are after).
That is a bit more difficulat and certainly requirs work in the registry.
I would not suggest you do this though, since you are basically interfering with the System functionality. Users of the machines may not appreciate this restriction. Still, if that is what you are after, let me know....I am investigating.
crystalsoftAuthor Commented:

Just for my vb application
Not for every windows programms
if form1 is maximized and timer is "00:00:00" User can not go to windows directly using Alt+Tab, first he need to take permission from server or administrator,
after form1 is minimized and timer is not   "00:00:00" then he can use alt+tab.

hope you understand this
Chris Raisin(Retired Analyst/Programmer)Commented:
You CAN turn off the Alt+Tab key by changing the value in a registry entry from 1 to 0
The key in the registry is "HKEY_CURRENT_USER\Control Panel\Desktop\CoolSwitch"
If the value in it is set to "0" then Alt+Tab is turned off, and if it is set to 1, it is turned back on.
You MUST reboot however after altering it for the setting to come into effect.
This is not advisable though, since if your program "crashes" the user may be left unable to use Alt+Tab again until you run your program again.

That's the bad news....
The good news is the following code does what you want, I think.
It is internal ONLY to your program and prevents the user from switching from the current form (in your program) on the desktop to any other form within your program on your desktop, or from minimizing the form or closing it.
Create a test Project with Two forms.
Form1 has startup position as "Default" , and Form2 as "Centre Screen"
Add a button to Form1  called "cmdShowForm" with the following within Form1
      Option Explicit

      Private Sub Form_Load()
        Form1.cmdShowForm.Caption = "Show Form 2"
     End Sub

     Private Sub cmdShowForm_Click()
           Form2.Show vbModal ' must be called as modal to prevent leaving the form
      End Sub
Add a textbox to Form2 called "txtTimeRemaining" and a "Timer" called "Timer1".
then add the following code to the internals of Form2

You will find that while the timer is counting down you cannot do anything within your program, although the user can still Alt+Tab to outside programs (as he/she should be able to do) , Of course you cannot force the active window in your program to remain on top of other programs...after all, the user can even turn the screen off if they wanted too!
(on a desktop anyway...lol)

This may give you some ideas on tweaking your program ......
Is that what you are after??

Option Explicit
Public dEndTime As Date

Private Sub Timer1_Timer()
  Dim m As Integer
  Dim s As Integer
  Dim h As Integer
  Dim dNow As Date
  dNow = Now
  h = DateDiff("h", dNow, dEndTime)
  m = DateDiff("n", dNow, dEndTime)
  s = DateDiff("s", dNow, dEndTime)
  txtTimeLeft.Text = Format(h, "00") + ":" + Format(m, "00") & ":" & Format(s, "00")
  If txtTimeLeft.Text = "00:00:00" Then
    Timer1.Interval = 0 'turn off timer
    Me.Enabled = True
  End If
End Sub
Private Sub Form_Load()
  dEndTime = DateAdd("s", 10, Now())
  Timer1.Interval = 1000
  Me.Enabled = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
  'just to make sure this cannot happen!
  If Me.txtTimeLeft.Text <> "00:00:00" Then
    Cancel = 1 'stop unload
  End If
End Sub

Open in new window

crystalsoftAuthor Commented:

Second suggestion will not work on my situation,

I checked First one but
First one is not working after restart also.
"HKEY_CURRENT_USER\Control Panel\Desktop\CoolSwitch"
value i set is 0
then i restarted computer
but still its ALT+TAB is active

Chris Raisin(Retired Analyst/Programmer)Commented:
Why won't second suggestion work? Do you mean thois is not what you want?
The example works beautifully on my machine.
crystalsoftAuthor Commented:
I mean second suggestion is not what i want.

what i want is
if form 1 windowstate  = 2 (Maximized) then ALT+TAB will not work and if form 1 windowstate = 1 (Minimized) then ALT+TAB will work.

i am developing an application for INTERNET CAFE.

Note: Form1 will minimized when administrator will give access from server.
crystalsoftAuthor Commented:
user can not access without taking permission from administrator or server

hope you understand.
crystalsoftAuthor Commented:

But sometimes it works and some time not, then i have to restart system. i want it work without restart system.
Please check code
one form
one check box (chkDisable)
One Module (modDisableLowLevelKeys)
'''''Form 1

Option Explicit

Dim hhkLowLevelKybd As Long

Private Sub chkDisable_Click()
    If chkDisable = vbChecked Then
        hhkLowLevelKybd = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, App.hInstance, 0)
        UnhookWindowsHookEx hhkLowLevelKybd
        hhkLowLevelKybd = 0
    End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
If hhkLowLevelKybd <> 0 Then UnhookWindowsHookEx hhkLowLevelKybd
End Sub

''''''''for module(modDisableLowLevelKeys)
Option Explicit

' Original code by Ovidiu Crisan
'       http://vbwork.4mg.com
' Addistions by Herman Eldering
'       http://Herman.Eldering.net/
' Free to use

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Const HC_ACTION = 0
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const WM_SYSKEYDOWN = &H104
Public Const WM_SYSKEYUP = &H105
Public Const VK_TAB = &H9
Public Const VK_ESCAPE = &H1B

Public Const VK_SHIFT = &H10    'SHIFT key
Public Const VK_CONTROL = &H11  'CTRL key
Public Const VK_MENU = &H12     'ALT key

Public Const VK_LWIN = &H5B  'Left Windows key (Microsoft® Natural® keyboard)
Public Const VK_RWIN = &H5C  'Right Windows key (Natural keyboard)
Public Const VK_APPS = &H5D  'Applications key (Natural keyboard)
'VK_LAUNCH_MAIL B4 Windows 2000: Start Mail key
'VK_LAUNCH_MEDIA_SELECT B5 Windows 2000: Select Media key
'VK_LAUNCH_APP1 B6 Windows 2000: Start Application 1 key
'VK_LAUNCH_APP2 B7 Windows 2000: Start Application 2 key

Public Const WH_KEYBOARD_LL = 13
Public Const LLKHF_ALTDOWN = &H20

    vkCode As Long
    scanCode As Long
    flags As Long
    time As Long
    dwExtraInfo As Long
End Type


Public Function LowLevelKeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
   Dim fEatKeystroke As Boolean
   If (nCode = HC_ACTION) Then
      If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Or wParam = WM_KEYUP Or wParam = WM_SYSKEYUP Then
         CopyMemory p, ByVal lParam, Len(p)
         fEatKeystroke = _
            p.vkCode = VK_LWIN Or _
            p.vkCode = VK_RWIN Or _
            p.vkCode = VK_APPS Or _
            p.vkCode = VK_CONTROL Or _
            p.vkCode = VK_SHIFT Or _
            p.vkCode = VK_MENU Or _
            ((GetKeyState(VK_CONTROL) And &H8000) <> 0) Or _
            ((p.flags And LLKHF_ALTDOWN) <> 0)
        End If
    End If
    If fEatKeystroke Then
        LowLevelKeyboardProc = -1
        LowLevelKeyboardProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)
    End If
End Function

Open in new window


Why do you want to do this?  (under what context?)
crystalsoftAuthor Commented:
i am developing an application for INTERNET CAFE.
You can use a low level keyboard hook, it's very easy...  Your writing in vb6?  I could give you a vb.net example, if you'd like...

crystalsoftAuthor Commented:
jake072:   Yeh i am writing in VB6,
 if you have example in vb6 or may be you can check above code i posted in
12/18/09 08:49 PM, ID: 26080943, some times its working and sometime not..

Basically, you want to create a low-level keyboard hook as in here

Then test to see if your app is in foreground as in here

Finally test the WindowState as in here

Mark WillsTopic AdvisorCommented:
Had to do this once myself to "capture" the user until they successfully logged on - so I guess it might be similar.

For me it was quite easy : http://support.microsoft.com/kb/226359 and it was a long time ago (might be able to search Microsoft given this link). It does look like Jake072 has given good links and probably is the updated version of the one I posted...

It would help if you described what you are trying to achieve. Simply saying "INTERNET CAFE" doesnt really describe the process all that well.

I am assuming it is your own Internet Cafe (or being contracted), otherwise it does sound a bit dubious as to what you are trying to achieve.

A lot of internet cafes set up a portal environment where the internet access is redirected to a login page - but sounds like you're not doing just internet.

If you could please explain a bit more, it will help a lot... If you can't explain in more detail, then it might be a bit questionable as to what you are doing (in terms of us helping you if it is not legitimate). So, please take the time to try to clarify the requirement...
Chris Raisin(Retired Analyst/Programmer)Commented:
I searched the Internet extensively and tried many things, but no success with an XP machine.
Now, if you were running under WindowsNT or versions earlier than XP, there is a solution which involves calls to the function SystemParametersInfo (which is part of the User32.dll")
but Microsoft obviously decided to remove this functions ability to turn off the Alt+Tab hotkey (and the "Alt+Esc" hotkey).
That function actually intercepts key input, and using it you specifiy the key combinatins as a screensaver call, with screensavers temporarily turned off (in other words, reasssigning the interrupt).
The onl;y other solution which others have said works is using the following code. I have simply block copied the code and have not tested it (off interstate for three weeks holiday in about 6 hours, so I will leave it up to you to look through...it may give you ideas).
No guarantees.....Happy Christmas

Option Explicit

' ***********************************
' Author : Imran Zaheer
' Email  : imraanz@mail.com
' Web    : www.imraanz.com
' Y2K
' Module : Contains declarations and functions for
'          vHotKeys.

Public Declare Function RegisterHotKey Lib "User32" (ByVal hwnd As Long, _
    ByVal ID As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Public Declare Function UnregisterHotKey Lib "User32" (ByVal hwnd As Long, _
    ByVal ID As Long) As Long

Declare Function SetWindowLong Lib "User32" Alias "SetWindowLongA" _
    (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Declare Function CallWindowProc Lib "User32" Alias "CallWindowProcA" _
    (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Public Const WM_HOTKEY = &H312
Public Const GWL_WNDPROC = -4

Public Const MOD_CTRL = &H2 'This example uses CTRL
Public Const MOD_SHFT = &H4
Public Const MOD_ALT = &H1

' VK_A thru VK_Z are the same as their ASCII equivalents: 'A' thru 'Z'
' VK_0 thru VK_9 are the same as their ASCII equivalents: '0' thru '9'
' and others are listed below

Public Const VK_NUMPAD0 = &H60
Public Const VK_NUMPAD1 = &H61
Public Const VK_NUMPAD2 = &H62
Public Const VK_NUMPAD3 = &H63
Public Const VK_NUMPAD4 = &H64
Public Const VK_NUMPAD5 = &H65
Public Const VK_NUMPAD6 = &H66
Public Const VK_NUMPAD7 = &H67
Public Const VK_NUMPAD8 = &H68
Public Const VK_NUMPAD9 = &H69
Public Const VK_MULTIPLY = &H6A
Public Const VK_ADD = &H6B
Public Const VK_SEPARATOR = &H6C
Public Const VK_SUBTRACT = &H6D
Public Const VK_DECIMAL = &H6E
Public Const VK_DIVIDE = &H6F
Public Const VK_F1 = &H70
Public Const VK_F2 = &H71
Public Const VK_F3 = &H72
Public Const VK_F4 = &H73
Public Const VK_F5 = &H74
Public Const VK_F6 = &H75
Public Const VK_F7 = &H76
Public Const VK_F8 = &H77
Public Const VK_F9 = &H78
Public Const VK_F10 = &H79 ' This example uses F10
Public Const VK_F11 = &H7A ' This example uses F11
Public Const VK_F12 = &H7B ' This example uses F12
Public Const VK_F13 = &H7C
Public Const VK_F14 = &H7D
Public Const VK_F15 = &H7E
Public Const VK_F16 = &H7F
Public Const VK_F17 = &H80
Public Const VK_F18 = &H81
Public Const VK_F19 = &H82
Public Const VK_F20 = &H83
Public Const VK_F21 = &H84
Public Const VK_F22 = &H85
Public Const VK_F23 = &H86
Public Const VK_F24 = &H87

Public glWinRet As Long

' Function : CallbackMsgs
' This functions is used as a parameter in the
' API SetWindowLong(), by AddresOf operator, so as to
' Subclass the form to get the Windows Callback msgs...
Public Function CallbackMsgs(ByVal wHwnd As Long, ByVal wmsg As Long, ByVal wp_id As Long, ByVal lp_id As Long) As Long
    If wmsg = WM_HOTKEY Then
        Call DoFunctions(wp_id)
        CallbackMsgs = 1
        Exit Function
    End If
    CallbackMsgs = CallWindowProc(glWinRet, wHwnd, wmsg, wp_id, lp_id)
End Function

' Sub : DoFunction
' Activated by the Function "CallbackMsgs()" whenever
' a hotkey is pressed.
Public Sub DoFunctions(ByVal vKeyID As Byte)
    ' Important Notes :
    ' Do not include any msgboxes or Modal forms in
    ' this procedure, else if you include then by
    ' pressing the Hotkey twice/thrice the application
    ' will be terminated abnormally.
    ' But if it is a requirement for you to include the
    ' Modal forms or msgbox in this procedure then put
    ' the RegisterHotKey() API before hiding the Form
    ' and put the UnRegisterHotKey() API before Showing
    ' the form.
    Form1.WindowState = 0
    ' When the Hotkey is pressed once
    ' check if the Dofunctions() has completed
    ' before the CallbackMsgs().
    ' This check is not required if the form is
    ' minimized in the SysTray ...
    If Form1.Visible = False Then
        Form1.WindowState = 0
    End If
    If vKeyID = 0 Then
        Form1.Label1.Caption = "1st HotKey Pressed !"
        If vKeyID = 1 Then
            Form1.Label1.Caption = "2nd HotKey Pressed !"
            Form1.Label1.Caption = "3rd HotKey Pressed !"
        End If
    End If
End Sub

Open in new window

Richard QuadlingSenior Software DeverloperCommented:
My approach to this (I developed an app which was used in our factory) was to create a user which loaded my app as the shell rather than loading Explorer.

Without Explorer there is no ALT+TAB.

This was set via a group policy.

The app contained the ability to reboot the PC.

Mark WillsTopic AdvisorCommented:
@rquadling - nice lateral approach - I like it :)
Richard QuadlingSenior Software DeverloperCommented:
Thank you.

We also used Kiosk mode for IE and used a touch screen rather than a full keyboard.

Both techniques worked for the factory quite well.

There are tools like http://www.autohotkey.com and http://www.phord.com/keyblock.html look useful and MS has a KB on doing something like this (http://support.microsoft.com/kb/161133)

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 10
  • 5
  • 3
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now