|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Feb 2004
Messaggi: 443
|
Acquisizione caratteri da tastiera sotto linux
Salve a tutti!!
Ho un problema nell'acquisizione singola dei tasti premuti da tastiera con il C++ (G++ in ambiente linux). Mi spiego, in Windows esiste una funzione fgetch() che "legge" un carattere alla volta senza aspettare che premi invio. In linux non esiste (non sono riuscito a trovare) una funzione simile che faccia al mio caso (infatti cin inserisce nello stream fino a quando premo invio ). Cosa posso fare ?? Saluti |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
stdin è line-buffered, devi renderla char-buffered (ed opzionalmente non bloccante). v. man termios, in particolare tcsetattr.
Ho postato del codice su questo argomento diverso tempo fa; se non riesci a trovarlo ti do una mano.
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Feb 2004
Messaggi: 443
|
Grazie per l'aiuto che mi stai dando
Ho trovato questo a riguardo tcsetattr() sets the parameters associated with the termi- nal (unless support is required from the underlying hard- ware that is not available) from the termios structure referred to by termios_p. optional_actions specifies when the changes take effect: Ma non ho capito bene cosa devo fare (mi sembra un po' complesso) Saluti |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Codice:
struct termios new_tty; struct termios old_tty; tcgetattr(fileno(stdin), &old_tty); new_tty = old_tty; new_tty.c_lflag &= ~ICANON; // Decommenta questa riga se vuoi togliere l'eco dei caratteri // new_tty.c_lflag &= ~ECHO; new_tty.c_cc[VMIN] = 1; new_tty.c_cc[VTIME] = 0; tcsetattr(fileno(stdin), TCSAFLUSH, &new_tty); Codice:
int cnt; ioctl(fileno(stdin), FIONREAD, &cnt); Codice:
char ch; read(fileno(stdin), &ch, 1);
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 |
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Feb 2004
Messaggi: 443
|
Grazie mille!!
Pero' ho un'altro problema : l'attesa di un carattere. Es. Pensa se devi creare un flipper che comandicon la tastiera in Linux Esiste un modo in linux per gestire cio' ?? Come si puo' creare un'interfaccia ad eventi se c'e' una funzione bloccante ?? Saluti ! |
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
|
|
|
|
|
|
#7 |
|
Senior Member
Iscritto dal: Feb 2004
Messaggi: 443
|
Esatto !!
Eh la borland fa funzioni veramente efficenti e semplici per il programmatore. Avresti dell'altro materiale a riguardo ?? Grazie mille per l'aiuto che mi hai dato finora. Mi piacerebbe specializzarmi di piu' con il C++ in linux con compilatori freeware potenti. Saluti! |
|
|
|
|
|
#8 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Mi sembra che ci sia già abbastanza materiale...hai guardato nei link ?
Codice:
#include <curses.h>
int main(){
WINDOW *w;
int c;
w = initscr();
cbreak(); /* no line buffering */
nodelay(w, TRUE); /* make getch call non blocking */
while(1){
if((c = getch()) != ERR) break; /* if(kbhit()) break; */
}
endwin();
return 0;
}
|
|
|
|
|
|
#9 | |
|
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Quote:
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 02:42.



















