Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    [C] Leggere un carattere per volta dalla tastiera

    Ciao a tutti, che funzione potrei usare per leggere quello che scrive l'utente sulla tastiera un carattere per volta? Mi spiego meglio: una funzione che dà il controllo all'utente, l'utente preme un tasto, e il controllo torna al programma dicendo che tasto è stato premuto... Ho provato a usare la getchar, ma finchè non viene premuto invio il controllo resta all'utente...

    Come si può fare??

    Grazie!!

  2. #2
    Con la getch(), _getch() o qualunque altra funzione equivalente.
    01010011 01100001 01101101 01110101 01100101 01101100 01100101 01011111 00110111 00110000
    All errors are undocumented features waiting to be discovered.

  3. #3
    Scusami ma mi sono dimenticato di dire che sto lavorando con Ubuntu in ANSI C...
    E lì ovviamente conio.h non esiste... cosa potrei usare?

  4. #4
    Non ho sufficienti esperienze in ambienti .NIX.
    Ma orientativamente dovresti usare le ncurses.
    Input
    To read a character from stdscr, use the function int getch(void).


    int ch = getch();

    No echoing. If you have called noecho(), the character ch will not be printed on the screen, otherwise it will. Disabling automatic echoing gives you more control over the user interface.

    No buffering. If you have called cbreak(void) each key the user hits is returned immediately by getch(). Otherwise the keys hit by the user are queued until a newline is read. Then calls to getch() take characters from the queue in FIFO manner until the queue is empty and the next whole line is read.

    No delaying. Usually a call to getch() waits until a key is hit. If you have called nodelay(stdscr, TRUE), then getch() will work in a non-blocking manner -- it will return ERR if the key input is not ready. This is usually useful for writing game-like programs, where the promptness of user response matters. For example


    int ch;
    nodelay(stdscr, TRUE);
    for (; {
    if ((ch = getch()) == ERR) {
    /* user hasn't responded
    ...
    */
    }
    else {
    /* user has pressed a key ch
    ...
    */
    }
    }
    Fonte
    01010011 01100001 01101101 01110101 01100101 01101100 01100101 01011111 00110111 00110000
    All errors are undocumented features waiting to be discovered.

  5. #5
    Questo codice funziona con Cygwin, quindi suppogno funzioni anche su Linux:

    codice:
    #include <stdio.h>
    
    int main(void){
     int c;
     do
     {
       c = _getch();
       printf("Carattere = %c\n", c);
     } while (c != 27); // Tasto ESC = Esci
     return 0;
    }
    "Se riesci a passare un pomeriggio assolutamente inutile in modo assolutamente inutile, hai imparato a vivere."

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.