2

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

kleash
  • 1,211
  • 1
  • 12
  • 31
  • Is it possible for you to post complete running program ? – Alpesh Jikadra May 02 '19 at 05:34
  • Sure, I am outside right now, will post the complete code in 6-7 hours.. – kleash May 02 '19 at 05:52
  • @AlpeshJikadra Added full source code: http://tpcg.io/zt3WfM – kleash May 03 '19 at 03:07
  • If the scanner presents itself as a keyboard, can't you just collect the keypresses? (Background: I've done this with a barcode scanner too) – Mark Jeronimus May 07 '19 at 13:26
  • @MarkJeronimus, That's what I thought initially. But it is more complex than that, my java program is running in the background and it's another overhead to capture keystrokes in background java process. – kleash May 07 '19 at 15:48
  • 1
    If one starts seaching for a list of scancodes, this turns out to be a duplicate. https://stackoverflow.com/questions/27075328/list-of-hex-keyboard-scan-codes-and-usb-hid-keyboard-documentation - scancodes are listed starting at page 53 of https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf – tevemadar May 07 '19 at 16:08

1 Answers1

3

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')

Richard Neish
  • 8,414
  • 4
  • 39
  • 69
Butiri Dan
  • 1,759
  • 5
  • 12
  • 18
  • So what you are saying is, I can read the byte array, discard 2 and 0 and get rest of the integers. For example: when I pressed A, after discarding I will get 4, similarly for A & B, I will get 4,5 and then I should add it in 0x61? Or is there any other way to parse above byte array(given in question) – kleash May 03 '19 at 02:59
  • The "2" here means you're pressing Shift. If you don't care about it, you can discard it. – Denis Tulskiy May 09 '19 at 04:44