How to read keypress from exclusive external USB keypad .
Posted: Saturday 6th June 2020 8:11pm
Hello gambasers
*trying to
-use USB external keypad
-make it exclusive to gambas program
-read keypress events
*problem
Cannot figure out how to use ioctl correct way
despite reading ioctl italian cdrom control gambas program.
I get no hold on how to write structure and pass correct arguments to ioctl.
*raw code with only essential code steps
' Gambas class file
Library "libc:6"
Public Extern ioctl(di As Integer, req As Integer, arg As Long) As Integer
Public usbdev As File
Public Struct input_event
ev As Byte
End Struct
Public Struct ev
type As Byte
code As Byte
value As Integer
End Struct
Public Enum EVIOCGNAME = &90
Public Enum EVIOCGRAB = &01
Public Sub Main
'raw code to show flow commands that works
'used lsusb to get usb device then vendor id and then get eventid
'---------chmod with root permission to use /dev/input/eventid
cmdshell = "sudo -S chmod 644 /dev/input/" & eventid
Shell "echo " & <yourpassword> & " | " & cmdshell
'----------open event assigned to keypad device
eventdev = "/dev/input/ & eventid"
usbdev = Open eventdev FOR READ WATCH
'----------Here i am stuck cannot grab device
'found in c program : result =ioctl(fd,EVIOCGRAB,1);
'gambas
result = ioctl(usbdev.handle, EVIOCGRAB,1)
'gives always -1 which is an error
'how to know what error and why
'----Here also stuck cannot get name from keypad device
'found in c program : result = ioctl(usbdev.handle,EVIOCGNAME(SizeOf(name)),name)
Tried many different ways each time error
I don't know how to declare the right way in the Gambas class and how to
translate -- EVIOCGNAME(SizeOf(name)),name -- into gambas syntax for ioctl.
End
Public Sub File_read()
'----Here i am stuck not able to read EV_KEY events and code
'if i press keypad then a file_read happens and gambas error is given
Read #usbdev, input_event.ev, -256
'Tried many different ways each time read error
value2 = input_event.ev[0].value
If (value2 <> " " And input_event.ev[1].value = 1 And input_event.ev[1].type = 1) Then
Message(" code: " & input_event.ev[1].code)
Endif
End
'-----------------------INFORMATION GATHERED-----------------
from input.h , keyboard.h , input-events-codes.h
This is the way you get the kernel generated information back
caused by the received input (eg. a keypress)
struct input_event {
struct timeval time;
__u16 type;
__u16 code;
__s32 value;
};
/* get event bits */
#define EVIOCGBIT(ev,len) _IOC(_IOC_READ, 'E', 0x20 + (ev), len)
/* I suppose i have to understand it this way
_IOC_READ = READ the /dev/input/eventX by file.handle from opened device eventfile
'E' = EVIOCGBIT
'ev' = EV_KEY 'here i will get ev[ ].code -> keypress code
'len' = predefined memory reserved bytes with info ?
*/
from input-event-codes.h
#define EV_KEY 0x01
'-----input.h -----------------------
/* get device name */
#define EVIOCGNAME(len) _IOC(_IOC_READ, 'E', 0x06, len)
/* Grab/Release device */
#define EVIOCGRAB _IOW('E', 0x90, int)
I tried to give as much information necessary but stripped to the essential, hope this is clear enough to see the errors
or to be able to show the path to straigthen out my lack of knowledge in the gambas world.
Thanks ahead
*trying to
-use USB external keypad
-make it exclusive to gambas program
-read keypress events
*problem
Cannot figure out how to use ioctl correct way
despite reading ioctl italian cdrom control gambas program.
I get no hold on how to write structure and pass correct arguments to ioctl.
*raw code with only essential code steps
' Gambas class file
Library "libc:6"
Public Extern ioctl(di As Integer, req As Integer, arg As Long) As Integer
Public usbdev As File
Public Struct input_event
ev As Byte
End Struct
Public Struct ev
type As Byte
code As Byte
value As Integer
End Struct
Public Enum EVIOCGNAME = &90
Public Enum EVIOCGRAB = &01
Public Sub Main
'raw code to show flow commands that works
'used lsusb to get usb device then vendor id and then get eventid
'---------chmod with root permission to use /dev/input/eventid
cmdshell = "sudo -S chmod 644 /dev/input/" & eventid
Shell "echo " & <yourpassword> & " | " & cmdshell
'----------open event assigned to keypad device
eventdev = "/dev/input/ & eventid"
usbdev = Open eventdev FOR READ WATCH
'----------Here i am stuck cannot grab device
'found in c program : result =ioctl(fd,EVIOCGRAB,1);
'gambas
result = ioctl(usbdev.handle, EVIOCGRAB,1)
'gives always -1 which is an error
'how to know what error and why
'----Here also stuck cannot get name from keypad device
'found in c program : result = ioctl(usbdev.handle,EVIOCGNAME(SizeOf(name)),name)
Tried many different ways each time error
I don't know how to declare the right way in the Gambas class and how to
translate -- EVIOCGNAME(SizeOf(name)),name -- into gambas syntax for ioctl.
End
Public Sub File_read()
'----Here i am stuck not able to read EV_KEY events and code
'if i press keypad then a file_read happens and gambas error is given
Read #usbdev, input_event.ev, -256
'Tried many different ways each time read error
value2 = input_event.ev[0].value
If (value2 <> " " And input_event.ev[1].value = 1 And input_event.ev[1].type = 1) Then
Message(" code: " & input_event.ev[1].code)
Endif
End
'-----------------------INFORMATION GATHERED-----------------
from input.h , keyboard.h , input-events-codes.h
This is the way you get the kernel generated information back
caused by the received input (eg. a keypress)
struct input_event {
struct timeval time;
__u16 type;
__u16 code;
__s32 value;
};
/* get event bits */
#define EVIOCGBIT(ev,len) _IOC(_IOC_READ, 'E', 0x20 + (ev), len)
/* I suppose i have to understand it this way
_IOC_READ = READ the /dev/input/eventX by file.handle from opened device eventfile
'E' = EVIOCGBIT
'ev' = EV_KEY 'here i will get ev[ ].code -> keypress code
'len' = predefined memory reserved bytes with info ?
*/
from input-event-codes.h
#define EV_KEY 0x01
'-----input.h -----------------------
/* get device name */
#define EVIOCGNAME(len) _IOC(_IOC_READ, 'E', 0x06, len)
/* Grab/Release device */
#define EVIOCGRAB _IOW('E', 0x90, int)
I tried to give as much information necessary but stripped to the essential, hope this is clear enough to see the errors
or to be able to show the path to straigthen out my lack of knowledge in the gambas world.
Thanks ahead