HOWTO: Intercept Keyboard Input from Visual BasicLast reviewed: March 3, 1998Article ID: Q177992 |
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 INFORMATIONYou 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. Why would you want to do this? One purpose of the keyboard hook could be to enforce data validation before the current control loses focus. Another purpose is to work around the difference in event sequence when using an access (or hot key) as opposed to a TAB character or mouse click. For additional information on this problem, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q74905 TITLE : PRB: Access Key Causes Different Event Order than Mouse ClickThe 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
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 on a system, but not using "pure" Visual Basic. The hook must be placed in a standard DLL; while Visual Basic 5.0 can create OLE DLLs, it cannot create standard DLLs. (c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Arsenio Locsin, Microsoft Corporation
|
Additional query words: subclass
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |