Hello,

Is it possible to disable the windows key when the application is running??

Suppose I am running a particular application....and if the user press the windows key or clicked on the windows key on the task bar it shud prompt a message box that user is not allowed to press or click the windows key....

Can anyone help me on this???

Recommended Answers

All 3 Replies

Member Avatar for DyO1

the winkey is hotkey of explorer.exe
there isn't a code for disabling it,but you can disable it manually from regedit?

You'll need to brush up on Registry Coding and use

To disable the Windows key, follow these steps:
1.Click Start, click Run, type regedt32, and then click OK.
2.On the Windows menu, click HKEY_LOCAL_ MACHINE on Local Machine.
3.Double-click the System\CurrentControlSet\Control folder, and then click the Keyboard Layout folder.
4.On the Edit menu, click Add Value, type in Scancode Map, click REG_BINARY as the Data Type, and then click OK.
5.Type 00000000000000000300000000005BE000005CE000000000 in the Data field, and then click OK.
6.Close Registry Editor and restart the computer.
To enable the Windows key, follow these steps:
1.Click Start, click Run, type regedt32, and then click OK.
2.On the Windows menu, click HKEY_LOCAL_ MACHINE on Local Machine.
3.Double-click the System\CurrentControlSet\Control folder, and then click the Keyboard Layout folder.
4.Right-click the Scancode Map registry entry, and then click Delete. Click Yes.
5.Close Registry Editor and restart the computer.

Danger Will Robinson, Danger!

This sounds like a case of fat fingers while playing a video game causing. ;)>

Yes, you can block the Windows Key via an application. It requires you to do a low level keyboard hook via the windows API's.

Imports System.Runtime.InteropServices

Public Class Form1
   'Choose either while running or while focused
   Private BlockingOption As BlockingOptions = BlockingOptions.WhileRuning

   Private Enum BlockingOptions
      WhileRuning
      WhileFormOrOneOfItsControlsIsFocused
   End Enum


   Private Event DontDoThat()
   Private Sub Form1_DontDoThat() Handles Me.DontDoThat
      MessageBox.Show("Don't press the windows key!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
   End Sub

   Private hook As New IntPtr
   Private Const VK_LWIN As UInt32 = &H5B 'Left WinKey
   Private Const VK_RWIN As UInt32 = &H5C 'Right WinKey
   Private Const WM_KEYDOWN As Int32 = &H100

   Private Delegate Function LowLevelKeyboardProc(ByVal nCode As Int32, _
                                                  ByVal wParam As Int32, _
                                                  <[In]()> ByVal lParam As IntPtr) As Int32

   Private KeyboardHookProcedure As LowLevelKeyboardProc



   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      KeyboardHookProcedure = New LowLevelKeyboardProc(AddressOf LLKeyboardProc)
      Dim lpdwProcessId As New IntPtr
      Dim modulehandle As IntPtr = GetModuleHandle(CStr(Nothing))
      hook = SetWindowsHookEx(HookType.WH_KEYBOARD_LL, KeyboardHookProcedure, modulehandle, 0)
   End Sub

   Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
      'make sure hook is released.  
      'It should timeout and become inactive is app closes without release, but
      'do it anyways.
      If hook <> IntPtr.Zero Then
         UnhookWindowsHookEx(hook)
      End If
   End Sub

   Public Function LLKeyboardProc(ByVal Code As Int32, ByVal msg As Int32, ByVal ptrTokbllStructure As IntPtr) As Int32
      'see: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644985(v=vs.85).aspx
      If Code >= 0 Then
         'its a code we can process
         'need to retrieve keyboard structure from memory
         Dim kbs As New KBDLLHOOKSTRUCT
         'kbs = CType(Marshal.PtrToStructure(ptrTokbllStructure, GetType(KBDLLHOOKSTRUCT)), KBDLLHOOKSTRUCT)
         Marshal.PtrToStructure(ptrTokbllStructure, kbs)

         Dim vkc As UInt32 = kbs.scanCode


         If ((BlockingOption = BlockingOptions.WhileRuning) _
              OrElse (Me.Focused OrElse Me.ActiveControl.Focused)) AndAlso _
            (vkc = VK_LWIN OrElse vkc = VK_RWIN) Then
            If msg = WM_KEYDOWN Then 'only raise event on keydown
               RaiseEvent DontDoThat() 'an event to prompt the user
            End If
            Return 1 ' return non zero value to prevent processing these keys
         End If


      End If
      Return CallNextHookEx(hook, Code, msg, ptrTokbllStructure)
   End Function


   <DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall, SetLastError:=True)> _
   Private Shared Function SetWindowsHookEx(ByVal hookType As HookType, _
                                            ByVal lpfn As LowLevelKeyboardProc, _
                                            ByVal hMod As IntPtr, _
                                            ByVal dwThreadId As UInteger) As IntPtr
      'see: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644990(v=vs.85).aspx
   End Function

   Enum HookType As Int32
      WH_JOURNALRECORD = 0 : WH_JOURNALPLAYBACK = 1 : WH_KEYBOARD = 2 : WH_GETMESSAGE = 3
      WH_CALLWNDPROC = 4 : WH_CBT = 5 : WH_SYSMSGFILTER = 6 : WH_MOUSE = 7
      WH_HARDWARE = 8 : WH_DEBUG = 9 : WH_SHELL = 10 : WH_FOREGROUNDIDLE = 11
      WH_CALLWNDPROCRET = 12 : WH_KEYBOARD_LL = 13 : WH_MOUSE_LL = 14
   End Enum

     <DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
   Public Shared Function UnhookWindowsHookEx(ByVal hhk As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
   End Function

   <DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
   Private Shared Function CallNextHookEx(ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As Int32, ByVal lParam As IntPtr) As Int32
   End Function

   <StructLayout(LayoutKind.Sequential)> _
   Public Class KBDLLHOOKSTRUCT
      Public vkCode As UInt32
      Public scanCode As UInt32
      Public flags As KBDLLHOOKSTRUCTFlags
      Public time As UInt32
      Public dwExtraInfo As UIntPtr
   End Class

   <Flags()> _
   Public Enum KBDLLHOOKSTRUCTFlags As UInt32
      LLKHF_EXTENDED = &H1
      LLKHF_INJECTED = &H10
      LLKHF_ALTDOWN = &H20
      LLKHF_UP = &H80
   End Enum

   <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
   Public Shared Function GetModuleHandle(ByVal lpModuleName As String) As IntPtr
   End Function
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.