Comunidad Gambas-es

Versión completa: Leyendo con la función externa ioctl() los códigos de las teclas pulsadas del teclado
Actualmente estas viendo una versión simplificada de nuestro contenido. Ver la versión completa con el formato correcto.
Os dejo este código para leer con la función externa ioctl() los códigos numericos de las teclas pulsadas del teclado.
Hace falta eliminar la protección del file-device del evento del teclado (por ejemplo: /dev/input/event4).

GAMBAS
  1. Library "libc:6"
  2.  
  3. Public Struct timeval
  4.   tv_sec As Long
  5.   tv_usec As Long
  6.  
  7. Public Struct input_event
  8.   time_ As Struct Timeval
  9.   type As Short
  10.   code As Short
  11.   value As Integer
  12.  
  13. Private Const _IOC_NRSHIFT As Integer = 0
  14. Private Const _IOC_NRBITS As Integer = 8
  15. Private _IOC_TYPESHIFT As Long = _IOC_NRSHIFT + _IOC_NRBITS
  16. Private Const _IOC_TYPEBITS As Long = 8
  17. Private _IOC_SIZESHIFT As Long = _IOC_TYPESHIFT + _IOC_TYPEBITS
  18. Private Const _IOC_SIZEBITS As Long = 14
  19. Private _IOC_DIRSHIFT As Long = _IOC_SIZESHIFT + _IOC_SIZEBITS
  20. Private Const _IOC_WRITE As Long = 1
  21. Private Const _IOC_READ As Long = 2
  22.  
  23. ' int ioctl(int __fd, unsigned long int __request, ...)
  24. ' Perform the I/O control operation specified by REQUEST on FD.
  25. Private Extern ioctl_name(__fd As Integer, __request As Long, arg As Byte[]) As Integer Exec "ioctl"
  26.  
  27. ' int ioctl(int __fd, unsigned long int __request, ...)
  28. ' Perform the I/O control operation specified by REQUEST on FD.
  29. Private Extern ioctl_grab(__fd As Integer, __request As Long, arg As Integer) As Integer Exec "ioctl"
  30.  
  31. ' ssize_t read (int __fd, void *__buf, size_t __nbytes)
  32. ' Read NBYTES into BUF from FD.
  33. Private Extern read_C(__fd As Integer, __buf As Input_event, __nbytes As Long) As Long Exec "read"
  34.  
  35.  
  36. Private ke As New Input_event   ' Structura "Input_event" ut "Globalis" nobis declaranda est !
  37.  
  38.  
  39. Public Sub Main()
  40.  
  41.   Dim keyboard_name As New Byte[256]
  42.   Dim rcode As Long
  43.  
  44. '' (En mi sistema el file-device de "keyboard" es "/dev/input/event4"):
  45.   kfl = Open "/dev/input/event4" For Read Watch
  46.   If kfl.Handle < 0 Then
  47.     kfl.Close
  48.     Error.Raise("Error !")
  49.     
  50.   rcode = ioctl_name(kfl.Handle, Eviocname(Asc("E"), &h06, keyboard_name.Count), keyboard_name)
  51.   Print "Reading From: \e[34m"; keyboard_name.ToString(0, keyboard_name.Find(0))
  52.  
  53.   rcode = ioctl_grab(kfl.Handle, Eviocgrab(Asc("E"), &h90, SizeOf(gb.Integer)), 1)
  54.   Print "\e[0mGetting exclusive access: "; IIf(rcode == 0, "\e[32mSUCCESS\e[0m", "\e[31mFAILURE\e[0m")
  55.  
  56.  
  57.  
  58. Public Sub File_Read()
  59.  
  60.   Dim rd As Integer
  61.  
  62.   rd = read_C(kfl.Handle, ke, Object.SizeOf(ke))
  63.    
  64.   If rd > -1 Then
  65.     Print "\e[1m\e[31mev\e[0m: "; Str(ke); " , \e[1mtime\e[0m: "; ke.time_.tv_sec; " , \e[1mtype\e[0m: "; ke.type; " , \e[1mcode\e[0m: "; ke.code; " , \e[1mvalue\e[0m: "; ke.value
  66.    
  67.  
  68.  
  69. Private Function Eviocname(type As Long, nr As Long, size As Long) As Long
  70.     
  71.   Return Shl(_IOC_READ, _IOC_DIRSHIFT) Or Shl(type, _IOC_TYPESHIFT) Or Shl(nr, _IOC_NRSHIFT) Or Shl(size, _IOC_SIZESHIFT)
  72.     
  73.  
  74. Private Function Eviocgrab(type As Long, nr As Long, size As Long) As Long
  75.     
  76.   Return Shl(_IOC_WRITE, _IOC_DIRSHIFT) Or Shl(type, _IOC_TYPESHIFT) Or Shl(nr, _IOC_NRSHIFT) Or Shl(size, _IOC_SIZESHIFT)
  77.     


Gracias Vuott!
Gracias Vuott. Ahora no se me ocurre en que lo puedo usar pero seguramente sera útil en algún momento.
Saludos.
Para saber un poco más sobre la función externa ioctl( ) con Gambas:

   https://www.gambas-it.org/wiki/index.php?title=Ioctl()
y
   https://www.gambas-it.org/wiki/index.php...ne_ioctl()