簡體   English   中英

C ncurses KEY_BACKSPACE 不起作用

[英]C ncurses KEY_BACKSPACE not working

我正在嘗試使用護士庫來處理此代碼。 我試圖讓它在我按下退格鍵時打印字母 i,但它似乎不起作用。 看起來很簡單。 它應該工作,但它不是。 我錯過了什么嗎? 提前致謝

#include <ncurses.h>
#include <stdio.h>

int main () {

  short ch;
  initscr();
  keypad(stdscr, TRUE);
  clear();
  noecho();
  ch = getch();

  while(ch != '\n') {
    if(ch == KEY_BACKSPACE) {
      mvaddch(90, 90, 'i');
    }
    ch = getch();
  }

  endwin();
}

NCurses 的健壯鍵盤處理程序至少捕獲三個潛在值:

short ch = getch();
...
switch (ch) {
...
case KEY_BACKSPACE:
case 127:
case '\b':
  /* Handle backspace here. */
  break;
...
}

原因是退格鍵會導致不同的返回值。 這取決於平台、終端和當前設置。

我也遇到過和你一樣的問題,做了一個小程序,輸出一個組合鍵的代碼,暫時解決了這個問題。

#include <stdio.h>
#include <ncurses.h>
#include <locale.h>
#include <wchar.h>

int main()
{
    setlocale(LC_CTYPE, ""); initscr(); raw(); noecho(); keypad(stdscr, TRUE);
    wint_t c;
    get_wch(&c);
    endwin();
    printf("Keycode: %d\n", c);
    return 0;
}

它在我的電腦上輸出 127 退格。 我只是在我的程序中的某個地方添加了一個 #define ALT_BACKSPACE 127 ,然后我就讀完了。

實際上,getch() 返回 int,但不返回 short 或 char。 嘗試使用 int 而不是 char,因為 KEY_BACKSPACE 包含超過 1 個字節。

另外,為什么不,考慮使用 wgetch(window) 而不是 getch():

int ch;
ch = wgetch(<here put your window handler>)

在這種情況下,對於 ch 也使用 int 或 uint32_t 而不是 short int,因為 wgetch() 返回的 BACKSPACE 鍵碼 (KEY_BACKSPACE) 也可以使用最多 4 個字節。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM