Reputation: 1319
I am reading a USB Keyboard (QR Code scanner) input using usb4java.
My code snippet looks like this:
byte[] data = new byte[16];
UsbPipe usbPipe = usbEndpoint.getUsbPipe();
if (usbPipe != null) {
if (!usbPipe.isOpen()) {
usbPipe.open();
}
if (usbPipe.isOpen()) {
UsbIrp usbIrp = usbPipe.createUsbIrp();
usbIrp.setData(data);
I have two questions:
1] On pressing A, byte array data is 2,0,0,0,0,0,0,0,2,0,4,0,0,0,0,0
On pressing AB, byte aray data is 2,0,0,0,0,0,0,0,2,0,4,0,0,0,0,0,2,0,5,0,0,0,0,0
How to convert it into character in java? i.e. get A or AB after conversion.
2] Currently, I am passing fixed size of byte array in above code snippet. For example, if I am expecting 1 char, I am passing 16 as size of byte array, for 2 characters 24 as size and so on. Is there any other elegant solution for making it dynamic?
PS: My byte array converter snippet:
StringBuffer sb = new StringBuffer();
for (byte b : data) {
sb.append(b);
sb.append(",");
}
String byteString = sb.toString();
return byteString;
Thanks for any help
EDIT 1: Full source code here: http://tpcg.io/zt3WfM
Upvotes: 2
Views: 1170
Reputation: 1772
Based on the documentation the format should be:
22 00 04 00 00 00 00 00
Offset Size Description
0 Byte Modifier keys status.
1 Byte Reserved field.
2 Byte Keypress #1.
3 Byte Keypress #2.
4 Byte Keypress #3.
5 Byte Keypress #4.
6 Byte Keypress #5.
7 Byte Keypress #6.
Based on the ASCII codes
// 'A' is 0x65
byte codeA = 0x04; // The code for A key
cahr a = 0x61 + codeA ;
byte codeX = 0x1B; // The code for X key
char x = 0x61 + code; // x == 'X'
System.out.println(a);
System.out.println(x);
Or you can use a Map(0x04, 'A')
Upvotes: 3