Individuare i tasti premuti della tastiera mediante le risorse esterne delle librerie ioctl.h e termio.h

Da Gambas-it.org - Wikipedia.

Utilizzando alcune risorse esterne dichiarate nei file header "/sys/ioctl.h" e "/usr/include/termio.h" e contenute nella libreria dinamica condivisa di C, è possibile intercettare ed individuare i tasti della tastiera premuti.

E' necessario richiamare in Gambas la libreria condivisa: "libc.so.6 ".

Mostriamo un esempio pratico:

Private Const STDINFD As Integer = 0


Library "libc:6"

Public Struct termio
  c_iflag As Short
  c_oflag As Short
  c_cflag As Short
  c_lflag As Short
  c_line As Byte
  c_cc[8] As Byte
End Struct

Private Const TCGETA As Integer = 21509
Private Const TCSETA As Integer = 21510
Private Const ICANON As Integer = 2
Private Const ECHO As Integer = 16

' int ioctl (int __fd, unsigned long int __request, ...)
' Perform the I/O control operation specified by REQUEST on FD.
Private Extern ioctl(__fd As Integer, __request As Integer, ter As Termio) As Integer


Public Sub Main()

  Dim c As Byte
  Dim param_ant As New Termio
  Dim params As Termio
  Dim fl As File
 
  Print "Premere un tasto..."
  
  ioctl(STDINFD, TCGETA, param_ant)
   
  params = param_ant
  params.c_lflag = params.c_lflag And Not (ICANON Or ECHO)
   
  params.c_cc[4] = 1
   
  ioctl(STDINFD, TCSETA, params)
  
  fl = Open "/proc/self/fd/0"
  Read #fl, c
  fl.Close
  
  ioctl(STDINFD, TCSETA, param_ant)
   
  Print "\rNumero del codice ASCII del tasto premuto: "; c; "  Carattere: "; Chr(c)
  
End


Riferimenti