如何使用 ncurses 获取 UTF-8 重音

How to use get UTF-8 accents with ncurses

我最近安装了新的 Windows 10 build 14393,我想使用新的 linux 子系统。所以我决定学习 ncurses,但我找不到如何从 getch 中获取带有 è 等重音字符的 UTF-8 代码。 所以这是我的代码:

#include <iostream>
#include <string>
#include <curses.h>

using namespace std;

int main(int argc, char **argv)
{
  char ch;

  setlocale(LC_ALL, "fr_FR.UTF-8");

  initscr();
  cbreak();
  noecho();

  printw("èèè\n"); // i can print accents

  ch = getch(); // if i press è it gives me 2 characters
  printw("%d", ch);
  ch = getch();
  printw("%d", ch);

  getch(); // wait for a key to exit
  endwin();

  return 0;
}

例如,如果我按 è,它会给我 2 个字符代码,即 -61-88,那么我如何从 UTF-8 table 哪个是 232? 我使用 ncursesw,这是我的 g++ 编译命令:

g++ file.cxx -o file -lncursesw

我的语言环境配置为 fr_FR.UTF-8。 感谢您的任何回答。

getch() 在 32 位 chtype 值中调用 returns 8 位字符(char 将无法正常工作,但这不是问题所在这个案例)。 UTF-8 是一种 字节编码,在这种情况下,ncurses 将 return 字节——每次调用一个字节。

要获取 Unicode(宽字符)值,请使用 get_wch,例如,

wchar_t ch;
get_wch(&ch);

您当然应该检查 get_wch 的 return 值,但它的 return 值将是 OKERR — 通过作为参数传递的指针 return 编辑字符。