Locking out keyboard keys (including the Windows key)

Posted on 2005-03-01
Medium Priority
Last Modified: 2008-03-03
I'm creating a program for a display at a convetion.  I would like to leave the mouse and keyboard available to the user.  But I need to lock out all the keys except for the ones I need for navigation.  These keys (and key shortcuts) which are being locked out MUST include the Windows + D and Windows + E.  Is there a way in VB to lock these out?
Question by:lillockey
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 37

Expert Comment

by:Harisha M G
ID: 13435422
LVL 28

Expert Comment

ID: 13436049

'=====bas module code====
Option Explicit
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
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
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Const WH_KEYBOARD = 2

Global hHook As Long

Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    If idHook < 0 Then
        KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
       If wParam = Asc("A") Then
          KeyboardProc = 1
          If lParam > 0 Then MsgBox "[A] key disabled"
          Exit Function
       End If
       KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
    End If
End Function

'========Form code======
Private Sub Form_Load()
    hHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, App.hInstance, App.ThreadID)
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Dim l As Long
    If hHook Then
        l = UnhookWindowsHookEx(hHook)
        hHook = 0
    End If
End Sub

'Run sample and press [A] key
LVL 10

Expert Comment

by:Jini Jose
ID: 13436845
did you want to bloack the keys when your programm is running ?

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 13443873
qmailjini - yes

Ark - sorry, but I need to be able to block the Windows button.

mgh_mgharish - I'm not gonna give up yet
LVL 28

Accepted Solution

Ark earned 2000 total points
ID: 13445463

Author Comment

ID: 13449901
Ark ... that was fabulous.
If I can detect the keys being pressed, then I can stop them.  I thank you for your sample.  It will help me greatly in making my program.

Expert Comment

ID: 14616111
I tested the Code posted by ARK above and whenever I press a Key different from 'A', the whole application frezes !

Any idea why that happens ?


Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question