ToUnicode function (winuser.h)
Translates the specified virtual-key code and keyboard state to the corresponding Unicode character or characters.
To specify a handle to the keyboard layout to use to translate the specified code, use the ToUnicodeEx function.
Syntax
int ToUnicode(
[in] UINT wVirtKey,
[in] UINT wScanCode,
[in, optional] const BYTE *lpKeyState,
[out] LPWSTR pwszBuff,
[in] int cchBuff,
[in] UINT wFlags
);
Parameters
[in] wVirtKey
Type: UINT
The virtual-key code to be translated. See Virtual-Key Codes.
[in] wScanCode
Type: UINT
The hardware scan code of the key to be translated. The high-order bit of this value is set if the key is up.
[in, optional] lpKeyState
Type: const BYTE*
A pointer to a 256-byte array that contains the current keyboard state. Each element (byte) in the array contains the state of one key. If the high-order bit of a byte is set, the key is down.
[out] pwszBuff
Type: LPWSTR
The buffer that receives the translated Unicode character or characters. However, this buffer may be returned without being null-terminated even though the variable name suggests that it is null-terminated.
[in] cchBuff
Type: int
The size, in characters, of the buffer pointed to by the pwszBuff parameter.
[in] wFlags
Type: UINT
The behavior of the function.
If bit 0 is set, a menu is active.
If bit 2 is set, keyboard state is not changed (Windows 10, version 1607 and newer)
All other bits (through 31) are reserved.
Return value
Type: int
The function returns one of the following values.
Return value | Description |
---|---|
|
The specified virtual key is a dead-key character (accent or diacritic). This value is returned regardless of the keyboard layout, even if several characters have been typed and are stored in the keyboard state. If possible, even with Unicode keyboard layouts, the function has written a spacing version of the dead-key character to the buffer specified by pwszBuff. For example, the function writes the character SPACING ACUTE (0x00B4), rather than the character NON_SPACING ACUTE (0x0301). |
|
The specified virtual key has no translation for the current state of the keyboard. Nothing was written to the buffer specified by pwszBuff. |
|
One character was written to the buffer specified by pwszBuff. |
|
Two or more characters were written to the buffer specified by pwszBuff. The most common cause for this is that a dead-key character (accent or diacritic) stored in the keyboard layout could not be combined with the specified virtual key to form a single character. However, the buffer may contain more characters than the return value specifies. When this happens, any extra characters are invalid and should be ignored. |
Remarks
The parameters supplied to the ToUnicode function might not be sufficient to translate the virtual-key code because a previous dead key is stored in the keyboard layout.
Typically, ToUnicode performs the translation based on the virtual-key code. In some cases, however, bit 15 of the wScanCode parameter can be used to distinguish between a key press and a key release.
Requirements
Minimum supported client | Windows 2000 Professional [desktop apps only] |
Minimum supported server | Windows 2000 Server [desktop apps only] |
Target Platform | Windows |
Header | winuser.h (include Windows.h) |
Library | User32.lib |
DLL | User32.dll |
See also
Conceptual
Reference