A better version of conio.h's getch() function that includes support for Unicode and can retrieve more keyboard combinations.
The getch() (and getche()) functions in conio.h are cumbersome and often require multiple calls to handle a single event. For example, getch() returns the value 224 (0xE0) after pressing a non-printable character, which is inconvenient. Additionally, getch() cannot handle Unicode characters properly, causing your program to crash and introducing a vulnerability. unicode-getch addresses these issues by using a struct to store character and control data separately, thus avoiding the need for complex filtering or repeated calls, while fixing the crashes caused by multiple bytes being written simultaneously.
- Handles Unicode characters correctly.
- Detects individual control keys (Alt, Control, Shift).
- Stores character and control data separately.
Note
This library is Windows-only. Depending on your compiler, you may need to link against user32 by adding -luser32 to your compiler options.
The program defines a struct InputUTF8 as follows:
typedef struct InputUTF8
{
long utf8char;
long flags;
} InputUTF8;utf8charcontains the retrieved Unicode character in UTF-8 Big Endian. For example, the lowercase Greek theta (θ) is represented as0xceb8in UTF-8 encoding.flagscontains information about pressed keys that are non-printable, where the lowest byte is the WinAPI virtual key code equivalent.
The most significant bits are toggled for modifier keys: 0x80000000 for Alt, 0x40000000 for Control, and 0x20000000 for Shift. This allows the program to capture both Unicode characters and control keys simultaneously (e.g., Alt + A).
Using the unicode-getch library is straightforward. Simply include the library with #include "unicode_getch.h". Below is an example demonstrating how to use unicode-getch. You only need to call unicode_getch() and store its result in an InputUTF8 struct.
#include "unicode_getch.h"
int main()
{
InputUTF8 uchr = {0};
while (1)
{
uchr = unicode_getch();
printf("UTF8Char: 0x%02lx\t(%c)\tCTRL: 0x%02lx\n", uchr.utf8char, uchr.utf8char, uchr.flags);
}
}Due to the internal use of UTF-16 by WinAPI, unicode-getch only supports codepoints up to U+FFFF. This means that some characters, like the grinning face emoji (😀), return the replacement character code (0xefbfbd), while others, like the peace symbol emoji (☮️), are supported. All Unicode characters with codepoints less than or equal to U+FFFF are supported by the WinAPI functions used, thus also by unicode-getch.
If you want to contribute to unicode-getch, feel free to fork the project and create a pull request with the changes you want to make, describing the changes you made. A modified version of this library is used in the rewritten version of Newtrodit.
Feel free to contact me on Discord (@anic17) or my server Program Dream.
Copyright © 2026 anic17 Software