Cosa fa Getch () in Turbo C?Sono sicuro che non sono valori ASCII per i tasti pressati
Domanda
Cosa è Gatch () in Turbo C ritorno?L'ho usato per l'inizializzazione dei tasti freccia per un programma, i valori restituiti da Getch () erano 77, 80, 72 e 75 che sono valori ASCII per alfabeti, che indica chiaramente che non sono valori ASCII.Se non sono valori ASCII, allora cosa sono?
Soluzione
getch()
fornito nel conio.h
non standard restituisce un int:
#include <conio.h>
int getch(void);
.
Dal riferimento:
A single character from the predefined standard input handle is read and returned.
The input is not buffered. If there is a character pending from ungetch
(see section ungetch), it is returned instead. The character is not echoed to the
screen. This function doesn't check for special characters like Ctrl-C.
If the standard input handle is connected to the console, any pending output in the
stdout and stderr streams is flushed before reading the input, if these streams are
connected to the console.
Return Value
The character.
Portability
ANSI/ISO C No
POSIX No
.
Il tuo problema con i tasti freccia è che non sono caratteri monoblocco .Per gestire i tasti freccia è necessario gestire i codici Multi-Byte .I numeri che stai ottenendo sono semplicemente uno dei due byte dei codici chiave.
Per un esempio di lettura dei codici vedere (Come leggere i tasti freccia) Cheat Code in C Programmazione (quando immesso dalla tastiera)
Altri suggerimenti
Getch () Funzione restituisce due tasti di tasti per i tasti freccia (e alcuni altri tasti speciali), restituisce innanzitutto 0 (0x00) o 224 (0xE0), quindi restituisce un codice che identifica il tasto pressato. Per i tasti freccia, restituisce il 224 seguito per la prima volta da 72 (su), 80 (giù), 75 (a sinistra) e 77 (a destra).Se vengono premuti i tasti freccia NUM-Pad (con Numlock OFF), Getch () restituisce prima 0 anziché 224.
Quindi, potresti fare qualcosa come:
char ch = getch ();
if (ch == 0 || ch == 224)
{
switch (getch ())
{
case 72:
/* Code for up arrow handling */
break;
case 80:
/* Code for down arrow handling */
break;
/* ... etc ... */
}
}
.
Si prega di notare che Getch () non è standardizzato in alcun modo e questi codici potrebbero variare dal compilatore al compilatore.