The information in this article applies to:
SUMMARY
In Microsoft Excel, you can create a macro that performs different
operations depending on which keys you press when you run the macro (for
example, if you want your macro to perform one operation when you press the
CTRL key and another if you don't press the CTRL key).
MORE INFORMATION
To create a macro that responds to different key states, use an IF
function. The IF function allows you to control your macro based on the
results of calls it makes to the Windows environment. These calls to the
Windows environment allow the IF function to identify the state of the keys
you want to use. To return the key state of one or more keys, use the
Windows function, GetKeyState().
-or- The GetKeyState() FunctionThe GetKeyState() function returns a number indicating the current state of a specific key at the time the DLL call is made. A negative number indicates that the key is pressed, a positive number indicates that the key is not pressed.The following GetKeyState() Visual Basic code is an example that returns the current state of a key and takes action based on the key state returned. Microsoft provides examples of Visual Basic procedures for illustration only, without warranty either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. This Visual Basic procedure is provided 'as is' and Microsoft does not guarantee that it can be used in all situations. Microsoft does not support modifications of this procedure to suit customer requirements for a particular purpose. Note that a line that is preceded by an apostrophe introduces a comment in the code--comments are provided to explain what the code is doing at a particular point in the procedure. Note also that an underscore character (_) indicates that code continues from one line to the next. You can type lines that contain this character as one logical line or you can divide the lines of code and include the line continuation character. For more information about Visual Basic for Applications programming style, see the "Programming Style in This Manual" section in the "Document Conventions" section of the "Visual Basic User's Guide." WARNING: The Declare Function statements listed in the following macro are sensitive. If used incorrectly, these functions may cause a general protection (GP) fault in Windows or cause serious software problems. 'Type the following two lines as a single line in the module Declare Function GetKeyState Lib "USER" (ByVal nVirtKey As Integer) _ As Integer
The above example macro checks the state of the following keys or key
combinations: CTRL+ALT, CTRL, and ALT. The macro displays an alert box
describing the key or key combination that is pressed when you run the
macro.
The argument used to call the DLL function GetKeyState() is a number indicating which key to test. The number used is the virtual key code that the GetKeyState function uses to identify which key to test. When you write your macro, you should be aware of the following:
REFERENCES
"Microsoft Windows Software Developers Kit Reference," Volume 1, pages
1-30, 4-183
Additional query words: call register keystate GPF
Keywords : kbprg |
Last Reviewed: October 20, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |