Admin Production ni-theme
Current Publication

GetKeyPressEventCharacter

LabWindows/CVI

GetKeyPressEventCharacter

int GetKeyPressEventCharacter (int eventData2);

Purpose

Call this function when you receive an EVENT_KEYPRESS in a panel or control callback to get the character that was pressed. This function returns 0 if the callback's eventData1 parameter contains a virtual key code instead of a character.

Example Code

The following example code demonstrates how to use this function to get the character the user pressed on the keyboard and then display this character and its corresponding character code on the user interface. This code can handle both one- and two-byte character entries. To avoid processing two-byte characters twice, this code does not process events that contain the lead byte of a two-byte character.

Note Note   This code contains CmbSetC, a macro in toolbox.h that sets the character at a given pointer. This code uses this macro to place the character into a buffer. The buffer is large enough to accommodate one- or two-byte characters.

int CVICALLBACK KeyCallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2)

{

int character;

char buffer[3];

switch (event)

{

case EVENT_KEYPRESS:

/* ignore leading bytes - process only one-byte character or trail byte of two-byte character */

if (!KeyPressEventIsLeadByte (eventData2)) {

/* obtain keypress information */

character = GetKeyPressEventCharacter (eventData2);

/* put the character into a buffer for display */

memset (buffer, 0, sizeof (buffer));

CmbSetC (buffer, character);

/* display character and corresponding character code on the user interface */

SetCtrlAttribute (panelHandle, PANEL_CHAR, ATTR_CTRL_VAL, buffer);

SetCtrlAttribute (panelHandle, PANEL_CHARCODE, ATTR_CTRL_VAL, character);

}

break;

}

return 0;

}

Parameters

Input
Name Type Description
eventData2 int The panel or control callback's eventData2 parameter.

Return Value

Name Type Description
character int A positive character value if the EVENT_KEYPRESS event is for a single-byte character, the lead byte of a two-byte character, or the trail byte of two-byte character. If the event is for a lead byte, the character value is only the lead byte of the character. If the event is for a trail byte, the character value is the complete two-byte character.

0 if the EVENT_KEYPRESS event is not for a single-byte character, the lead byte of a two-byte character, or the trail byte of two-byte character. Negative values indicate that an error occurred.

Additional Information

Library: User Interface Library

Include file: userint.h

LabWindows/CVI compatibility: LabWindows/CVI 6.0 and later

Example

Refer to userint\multikey.cws for an example of using the GetKeyPressEventCharacter function.