GetKey
int GetKey (void);
Purpose
Waits for the user to press a key and then returns the key code as an integer value.
If the user has already pressed a key, the key value is returned immediately.
![]() |
Note GetKey detects keystrokes only in the Standard I/O window or in the console window of a Windows console application. GetKey does not detect keystrokes in windows you create with the User Interface Library.
(Linux) GetKey detects keystrokes only in the Standard I/O window. You must first call the SetStdioPort function to specify to use the Standard I/O window. |
The values GetKey returns are the same as the key values the User Interface Library uses. Refer to userint.h. The following table shows examples of keystrokes and the values GetKey returns for them.
Example Keystrokes and GetKey Return Values
Keystroke | Return Value |
<b> | 'b' |
<Ctrl-b> | (VAL_MENUKEY_MODIFIER | 'B') |
<F4> | VAL_F4_VKEY |
<Shift-F4> | (VAL_SHIFT_MODIFIER | VAL_F4_VKEY) |
Example Code
/* Give the user a chance to quit the program. */
int k;
FmtOut ("Enter 'q' to quit, any other key to continue");
k = GetKey ();
if ((k == 0x0051) || (k == 0x0071)) /* q or Q */
exit (0);
Parameters
None.
Return Value
Name | Type | Description | ||
keyCode | int | The value representing the key pressed by the user. Key codes can be formed by either an ASCII character, a virtual key, or by a logical OR operation on values representing a modifier key and either an ASCII character or a virtual key. A modifier key is a <Shift> key, <Alt> key, or <Ctrl> key. The following constants represent the modifier key: VAL_SHIFT_MODIFIER ASCII characters are represented by a literal character, for example, 'A' or 'D'. Multibyte characters are returned as two consecutive key codes. The first key code represents the lead byte while the second key code is the trail byte of the multibyte character. As a result you have to call GetKey twice in a multibyte setting to obtain the entire multibyte character. Virtual keys are non-ASCII keys represented by the following key codes: VAL_FWD_DELETE_VKEY
When the VAL_MENUKEY_MODIFIER is used in conjunction with a letter, the letter is always represented in its uppercase form. |
Additional Information
Library: Utility Library
Include file: utility.h
LabWindows/CVI compatibility: LabWindows/CVI 3.0 and later