将USB键盘数据从字节数组转换为字符串USB4Java
我正在使用usb4java读取USB键盘(二维码扫描仪)输入 我的代码片段如下所示:将USB键盘数据从字节数组转换为字符串USB4Java,java,arrays,byte,usb4java,Java,Arrays,Byte,Usb4java,我正在使用usb4java读取USB键盘(二维码扫描仪)输入 我的代码片段如下所示: byte[] data = new byte[16]; UsbPipe usbPipe = usbEndpoint.getUsbPipe(); if (usbPipe != null) { if (!usbPipe.isOpen()) { usbPipe.open(); } if (usbPipe.isOpen()) { UsbIrp usbIrp = u
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);
我有两个问题:
1] 按A键时,字节数组数据为2,0,0,0,0,0,0,2,0,4,0,0,0,0,0按AB键时,字节aray数据为2,0,0,0,0,0,0,2,0,4,0,0,0,0,0,2,0,5,0,0,0,0,0,0,0,0 如何在java中将其转换为字符?i、 e.转换后获得A或AB 2] 目前,我正在上面的代码片段中传递固定大小的字节数组。例如,如果我期望1个字符,我将16作为字节数组的大小传递,2个字符24作为大小传递,依此类推。有没有其他优雅的解决方案让它充满活力 PS:My byte array转换器代码段:
StringBuffer sb = new StringBuffer();
for (byte b : data) {
sb.append(b);
sb.append(",");
}
String byteString = sb.toString();
return byteString;
谢谢你的帮助
编辑1:此处的完整源代码:基于以下格式:
22 00 04 00 00 00 00 00
基于
或者你可以使用地图(0x04,'a')你可以发布完整的运行程序吗?当然,我现在在外面,将在6-7小时内发布完整的代码。@AlpeshJikadra添加了完整的源代码:如果扫描仪本身是一个键盘,你就不能收集按键吗?(背景:我也用条形码扫描器完成了这项工作)如果一个人开始搜索扫描码列表,结果会是重复的。-扫描码从的第53页开始列出,所以您要说的是,我可以读取字节数组,丢弃2和0,并获得其余的整数。例如:当我按下A键时,丢弃后我将得到4,类似地,对于A&B,我将得到4,5,然后我应该将其添加到0x61中?或者有没有其他方法来解析上面的字节数组(有问题的给出)这里的“2”表示您正在按Shift键。如果你不在乎它,你可以丢弃它。
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.
// '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);