Differenze tra le versioni di "Individuare i tasti premuti della tastiera mediante le risorse esterne dichiarate in termios.h"
Da Gambas-it.org - Wikipedia.
(Una versione intermedia di uno stesso utente non è mostrata) | |||
Riga 1: | Riga 1: | ||
Utilizzando alcune risorse dichiarate nel file header "''/usr/include/termios.h''", è possibile intercettare ed individuare i tasti premuti della tastiera. | Utilizzando alcune risorse dichiarate nel file header "''/usr/include/termios.h''", è possibile intercettare ed individuare i tasti premuti della tastiera. | ||
− | E' necessario richiamare in Gambas la libreria | + | E' necessario richiamare in Gambas la libreria condivisa: "''libc.so.6'' ". |
− | |||
Mostriamo un esempio pratico: | Mostriamo un esempio pratico: | ||
Riga 35: | Riga 34: | ||
'''Public''' Sub Main() | '''Public''' Sub Main() | ||
− | + | ||
− | + | Dim b As Byte | |
− | + | ||
+ | <FONT Color=gray>' ''Avvia un ciclo infinito:''</font> | ||
Do | Do | ||
b = Getchar() | b = Getchar() | ||
Print "\rPremuto il tasto: " & Chr(b); " - Codice ASCII: "; b | Print "\rPremuto il tasto: " & Chr(b); " - Codice ASCII: "; b | ||
Loop | Loop | ||
− | + | ||
'''End''' | '''End''' | ||
− | |||
'''Private''' Function Getchar() As Byte | '''Private''' Function Getchar() As Byte | ||
− | + | Dim trm As New Termios | |
− | + | Dim b As Byte | |
− | + | Dim i As Integer | |
− | + | Dim fl As File | |
− | + | ||
i = tcgetattr(0, trm) | i = tcgetattr(0, trm) | ||
If i < 0 Then Error.Raise("Errore alla funzione 'tcgetattr()' !") | If i < 0 Then Error.Raise("Errore alla funzione 'tcgetattr()' !") | ||
Riga 80: | Riga 79: | ||
Return b | Return b | ||
+ | '''End''' | ||
+ | |||
+ | |||
+ | Un altro esempio: | ||
+ | Library "libc:6" | ||
+ | |||
+ | Public Struct termios | ||
+ | c_iflag As Integer | ||
+ | c_oflag As Integer | ||
+ | c_cflag As Integer | ||
+ | c_lflag As Integer | ||
+ | c_line As Byte | ||
+ | c_cc[32] As Byte | ||
+ | c_ispeed As Integer | ||
+ | c_ospeed As Integer | ||
+ | End Struct | ||
+ | |||
+ | Private Const STDIN_FILENO As Integer = 0 | ||
+ | Private Const TCSANOW As Integer = 0 | ||
+ | Private Const ICANON As Integer = 2 | ||
+ | Private Const ECHO As Integer = 16 | ||
+ | |||
+ | <FONT Color=gray>' ''int tcgetattr (int __fd, struct termios *__termios_p)'' | ||
+ | ' ''Put the state of FD into *TERMIOS_P.''</font> | ||
+ | Private Extern tcgetattr(__fd As Integer, __termios_p As Termios) As Integer | ||
+ | |||
+ | <FONT Color=gray>' ''int tcsetattr (int __fd, int __optional_actions, const struct termios *__termios_p) | ||
+ | ' ''Set the state of FD to *TERMIOS_P.''</font> | ||
+ | Private Extern tcsetattr(__fd As Integer, __optional_actions As Integer, __termios_p As Termios) As Integer | ||
+ | |||
+ | |||
+ | '''Public''' Sub Main() | ||
+ | |||
+ | Dim t As Byte | ||
+ | |||
+ | Do | ||
+ | set_mode(1) | ||
+ | While Not t | ||
+ | t = get_key() | ||
+ | Wend | ||
+ | Print "\rTasto "; t;; Chr(t) | ||
+ | t = 0 | ||
+ | Loop | ||
+ | |||
+ | '''End''' | ||
+ | |||
+ | '''Private''' Procedure set_mode(wk As Integer) | ||
+ | |||
+ | Dim vet, nov As New Termios | ||
+ | |||
+ | If 0 == wk Then | ||
+ | With vet | ||
+ | .c_lflag = .c_lflag Or ICANON | ||
+ | .c_lflag = .c_lflag Or ECHO | ||
+ | End With | ||
+ | Return | ||
+ | Endif | ||
+ | |||
+ | tcgetattr(STDIN_FILENO, vet) | ||
+ | nov = vet | ||
+ | nov.c_lflag = nov.c_lflag And (Not (ICANON Or ECHO)) | ||
+ | tcsetattr(STDIN_FILENO, TCSANOW, nov) | ||
+ | |||
+ | '''End''' | ||
+ | |||
+ | '''Private''' Function get_key() As Byte | ||
+ | |||
+ | Dim fl As File | ||
+ | Dim b As Byte | ||
+ | |||
+ | fl = Open "/dev/stdin" | ||
+ | Read #fl, b | ||
+ | fl.Close | ||
+ | |||
+ | set_mode(0) | ||
+ | |||
+ | Return b | ||
+ | |||
'''End''' | '''End''' |
Versione attuale delle 12:47, 6 gen 2022
Utilizzando alcune risorse dichiarate nel file header "/usr/include/termios.h", è possibile intercettare ed individuare i tasti premuti della tastiera.
E' necessario richiamare in Gambas la libreria condivisa: "libc.so.6 ".
Mostriamo un esempio pratico:
Library "libc:6" Public Struct termios c_iflag As Integer c_oflag As Integer c_cflag As Integer c_lflag As Integer c_line As Byte c_cc[32] As Byte c_ispeed As Integer c_ospeed As Integer End Struct Private Const TCSANOW As Integer = 0 Private Const TCSADRAIN As Integer = 1 Private Const ICANON As Integer = 2 Private Const VTIME As Integer = 5 Private Const VMIN As Integer = 6 Private Const ECHO As Integer = 16 ' int tcgetattr (int __fd, struct termios *__termios_p) ' Put the state of FD into *TERMIOS_P. Private Extern tcgetattr(__fd As Integer, __termios_p As Termios) As Integer ' int tcsetattr (int __fd, int __optional_actions, const struct termios *__termios_p) ' Set the state of FD to *TERMIOS_P. Private Extern tcsetattr(__fd As Integer, __optional_actions As Integer, __termios_p As Termios) As Integer Public Sub Main() Dim b As Byte ' Avvia un ciclo infinito: Do b = Getchar() Print "\rPremuto il tasto: " & Chr(b); " - Codice ASCII: "; b Loop End Private Function Getchar() As Byte Dim trm As New Termios Dim b As Byte Dim i As Integer Dim fl As File i = tcgetattr(0, trm) If i < 0 Then Error.Raise("Errore alla funzione 'tcgetattr()' !") With trm .c_lflag = .c_lflag And (Not ICANON) .c_lflag = .c_lflag And (Not ECHO) .c_cc[VMIN] = 1 .c_cc[VTIME] = 0 End With i = tcsetattr(0, TCSANOW, trm) If i < 0 Then Error.Raise("Errore alla funzione 'tcsetattr()' !") fl = Open "/proc/self/fd/0" Read #fl, b fl.Close With trm .c_lflag = .c_lflag Or ICANON .c_lflag = .c_lflag Or ECHO End With i = tcsetattr(0, TCSADRAIN, trm) If i < 0 Then Error.Raise("Errore alla funzione 'tcsetattr()' !") Return b End
Un altro esempio:
Library "libc:6" Public Struct termios c_iflag As Integer c_oflag As Integer c_cflag As Integer c_lflag As Integer c_line As Byte c_cc[32] As Byte c_ispeed As Integer c_ospeed As Integer End Struct Private Const STDIN_FILENO As Integer = 0 Private Const TCSANOW As Integer = 0 Private Const ICANON As Integer = 2 Private Const ECHO As Integer = 16 ' int tcgetattr (int __fd, struct termios *__termios_p) ' Put the state of FD into *TERMIOS_P. Private Extern tcgetattr(__fd As Integer, __termios_p As Termios) As Integer ' int tcsetattr (int __fd, int __optional_actions, const struct termios *__termios_p) ' Set the state of FD to *TERMIOS_P. Private Extern tcsetattr(__fd As Integer, __optional_actions As Integer, __termios_p As Termios) As Integer Public Sub Main() Dim t As Byte Do set_mode(1) While Not t t = get_key() Wend Print "\rTasto "; t;; Chr(t) t = 0 Loop End Private Procedure set_mode(wk As Integer) Dim vet, nov As New Termios If 0 == wk Then With vet .c_lflag = .c_lflag Or ICANON .c_lflag = .c_lflag Or ECHO End With Return Endif tcgetattr(STDIN_FILENO, vet) nov = vet nov.c_lflag = nov.c_lflag And (Not (ICANON Or ECHO)) tcsetattr(STDIN_FILENO, TCSANOW, nov) End Private Function get_key() As Byte Dim fl As File Dim b As Byte fl = Open "/dev/stdin" Read #fl, b fl.Close set_mode(0) Return b End