QT HID 条码阅读器在 textinput 和 Keys.onPressed 内有不同的击键

问题描述

总而言之,我有一个条形码阅读器 MC3300(不相关),我有我的条形码阅读应用程序,它读取的一些字符与条形码内的字符不同,它们是:

  • ascii 41 读作 32
  • ascii 62 读作 48
  • ascii 30 读作 65535
  • ascii 4 读作 65535 等等...

但是像字母或数字这样的常见字符它可以正确读取。

但是,当我打开设备上的任何记事本并扫描它,然后导出到 txt 并遍历每个字符时,它会正确读取... 因此它必须是我的应用程序中的某些设置...

我可以要求澄清一下吗,例如我应该如何实施?

谢谢

编辑:我有这个 DMC:

"[)>06V00000596323S00000407141TIA1315JV2P634-00650-0020PQ100014Z16Z12D230903"

在 ASCII 中有哪些:

94 41 62 30 48 54 29 86 48 48 48 48 48 53 57 54 51 50 29 51 83 48 48 48 48 48 52 48 55 49 52 29 49 84 73 65 49 51 49 53 74 86 50 29 80 54 51 52 45 48 48 54 53 48 45 48 48 29 50 48 80 29 81 49 48 48 48 29 49 52 90 29 49 54 90 49 29 49 50 68 50 51 48 57 48 51 30 4

而这些 ASCII 是从以下输出的:

TextField {
    onTextChanged: {
         console.log(text.charat(text.length - 1)+ " ("+text.charCodeAt(text.length - 1)+"))
    }
}
  • 所以当我扫描 TextField 上的条码时,读取是正确的!

但是,如果我像这样使用它:

property string scannedValue: ""
Item {
    focus: true
    Keys.onpressed: { scannedValue += String.fromCharCode(event.key); }
}
onScannedValueChanged: {
    console.log(scannedValue .charat(scannedValue.length - 1)+ " ("+scannedValue .charCodeAt(scannedValue.length - 1)+"))
}

我有非常不同的结果......这将是:

"[)>06V00000596323S00000407141TIA1315JV2P634-00650-0020PQ100014Z16Z12D230903 Q100014 Z16 Z112 D230903"

并在 ASCII 中:

91 32 48 32 46 65535 48 54 65535 32 86 48 48 48 48 48 53 57 54 51 50 65535 51 32 83 48 48 48 48 48 52 48 55 49 52 65535 49 32 84 32 73 32 65 49 51 49 53 32 74 32 86 50 65535 32 4 54 51 52 45 48 48 54 53 48 45 48 48 65535 50 48 32 4 65535 32 81 49 48 48 48 65535 49 52 32 90 65535 49 54 32 90 49 65535 49 50 32 68 50 51 48 57 48 51 65535 65535
  • 这是不正确的:(

因此,这些差异来自哪里,主要是如何删除它们,以便获得与在 TextField 中直接扫描时相同的结果?

解决方法

不要使用 event.key - 这是键盘代码而不是 ASCII 值。尝试改用 event.text

更多信息在这里:

https://doc.qt.io/qt-5/qml-qtquick-keyevent.html#text-prop