如何在ncurses中使用get UTF-8重音符号

How to use get UTF-8 accents with ncurses

本文关键字:UTF-8 get 音符 符号 ncurses      更新时间:2023-10-16

我最近安装了新的Windows 10版本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;
}

例如,如果我按è,它会给我两个字符的代码,即-61-88,那么我如何从UTF-8表中获得它的代码,也就是232?我使用ncursesw,这里是我的g++命令来编译:

g++ file.cxx -o file -lncursesw

我的区域设置配置为fr_fr。UTF-8。谢谢你的回答。

getch()调用在32位chtype值中返回8位字符(char工作不好,但在这种情况下不是问题)。UTF-8是字节编码,在这种情况下,ncurses将返回字节—每次呼叫一个。

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

wchar_t ch;
get_wch(&ch);

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