Skip to content

anic17/unicode-getch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

unicode-getch

A better version of conio.h's getch() function that includes support for Unicode and can retrieve more keyboard combinations.

Why Use unicode-getch?

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.

Features

  • Handles Unicode characters correctly.
  • Detects individual control keys (Alt, Control, Shift).
  • Stores character and control data separately.

Usage

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;
  • utf8char contains the retrieved Unicode character in UTF-8 Big Endian. For example, the lowercase Greek theta (θ) is represented as 0xceb8 in UTF-8 encoding.
  • flags contains 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).

Example

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);
    }
}

Technical Limitation

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.

Contributing

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.

Contact

Feel free to contact me on Discord (@anic17) or my server Program Dream.

Copyright © 2026 anic17 Software

About

Better version of conio.h's getch() function that includes full support for Unicode and can retrieve more keyboard combinations

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages