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).
Código:
Library "libc:6"
Public Struct timeval
tv_sec As Long
tv_usec As Long
End Struct
Public Struct input_event
time_ As Struct Timeval
type As Short
code As Short
value As Integer
End Struct
Private Const _IOC_NRSHIFT As Integer = 0
Private Const _IOC_NRBITS As Integer = 8
Private _IOC_TYPESHIFT As Long = _IOC_NRSHIFT + _IOC_NRBITS
Private Const _IOC_TYPEBITS As Long = 8
Private _IOC_SIZESHIFT As Long = _IOC_TYPESHIFT + _IOC_TYPEBITS
Private Const _IOC_SIZEBITS As Long = 14
Private _IOC_DIRSHIFT As Long = _IOC_SIZESHIFT + _IOC_SIZEBITS
Private Const _IOC_WRITE As Long = 1
Private Const _IOC_READ As Long = 2
' int ioctl(int __fd, unsigned long int __request, ...)
' Perform the I/O control operation specified by REQUEST on FD.
Private Extern ioctl_name(__fd As Integer, __request As Long, arg As Byte[]) As Integer Exec "ioctl"
' int ioctl(int __fd, unsigned long int __request, ...)
' Perform the I/O control operation specified by REQUEST on FD.
Private Extern ioctl_grab(__fd As Integer, __request As Long, arg As Integer) As Integer Exec "ioctl"
' ssize_t read (int __fd, void *__buf, size_t __nbytes)
' Read NBYTES into BUF from FD.
Private Extern read_C(__fd As Integer, __buf As Input_event, __nbytes As Long) As Long Exec "read"
Private ke As New Input_event ' Structura "Input_event" ut "Globalis" nobis declaranda est !
Private kfl As File
Public Sub Main()
Dim keyboard_name As New Byte[256]
Dim rcode As Long
'' (En mi sistema el file-device de "keyboard" es "/dev/input/event4"):
kfl = Open "/dev/input/event4" For Read Watch
If kfl.Handle < 0 Then
kfl.Close
Error.Raise("Error !")
Endif
rcode = ioctl_name(kfl.Handle, Eviocname(Asc("E"), &h06, keyboard_name.Count), keyboard_name)
Print "Reading From: \e[34m"; keyboard_name.ToString(0, keyboard_name.Find(0))
rcode = ioctl_grab(kfl.Handle, Eviocgrab(Asc("E"), &h90, SizeOf(gb.Integer)), 1)
Print "\e[0mGetting exclusive access: "; IIf(rcode == 0, "\e[32mSUCCESS\e[0m", "\e[31mFAILURE\e[0m")
Print
End
Public Sub File_Read()
Dim rd As Integer
rd = read_C(kfl.Handle, ke, Object.SizeOf(ke))
If rd > -1 Then
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
Endif
End
Private Function Eviocname(type As Long, nr As Long, size As Long) As Long
Return Shl(_IOC_READ, _IOC_DIRSHIFT) Or Shl(type, _IOC_TYPESHIFT) Or Shl(nr, _IOC_NRSHIFT) Or Shl(size, _IOC_SIZESHIFT)
End
Private Function Eviocgrab(type As Long, nr As Long, size As Long) As Long
Return Shl(_IOC_WRITE, _IOC_DIRSHIFT) Or Shl(type, _IOC_TYPESHIFT) Or Shl(nr, _IOC_NRSHIFT) Or Shl(size, _IOC_SIZESHIFT)
End