The information in this article applies to:
SUMMARYThis article demonstrates how to intercept input from the keyboard before it reaches your Visual Basic application. Most keyboard input can be readily intercepted by a Visual Basic application by using the KeyDown event of the form or control. The technique shown here allows you how to handle otherwise unavailable key combinations such as the TAB key or access key combinations. MORE INFORMATION
You can create a keyboard hook that intercepts all keyboard input directed
to a given thread. To create this hook, you must replace the default
KeyboardProc() function with a KeyboardProc() of your own. The AddressOf()
construct introduced in Visual Basic 5.0 makes this possible. Within your
KeyboardProc(), you must either pass along the keyboard input you receive
or delete it. You cannot change the contents of the message you receive.
Q74905 PRB: Access Key Causes Different Event Order than Mouse Click The next section illustrates how to create a sample application that works around this behavior by intercepting the access key, setting the focus, and then causing a mouse-click event to occur. Please note that this does not resolve all issues with the key order problems. For example, calling Message boxes from an event affects the event order as well. WARNING: Failure to unhook the keyboard hook before exiting your application will result in errors, Invalid Page Faults, and data loss. This is caused by the system pointing to the your KeyboardProc() function, which no longer exists because it has not been notified of the change. Always unhook the keyboard upon exiting the application. This is especially important while debugging a program that uses this technique within the Microsoft Visual Basic 5.0 Development Environment. Clicking the End button or selecting End from the Run menu without unhooking will cause an Invalid Page Fault and close Microsoft Visual Basic without giving you a chance to save your code. Steps to Create Sample Project
The text box should have the focus. Click the command button and note the sequence of events that occur as shown in the debug window:
Set the focus to the text box and press the ALT+C keys and note that the
same events occur.
NotesHooks do not always behave the same way in the IDE as they do in an EXE. Make certain that you test your solution in an EXE before you move on to other parts of your project.It is possible to intercept all of the keyboard input from all of the applications running on a system, but not using "pure" Visual Basic. The hook must be placed in a standard DLL; while Visual Basic can create OLE DLLs, it cannot create standard DLLs. © Microsoft Corporation 1997, All Rights Reserved. Additional query words: subclass
Keywords : kbVBp500 kbVBp600 kbGrpVB VBKBWinAPI |
Last Reviewed: January 5, 2000 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |