@@ -26,26 +26,51 @@ final class HIDBarcodeParser {
2626 /// Process a key press event
2727 /// - Parameter key: The key that was pressed
2828 func processKeyPress( _ key: UIKey ) {
29- let currentTime = timeProvider. now ( )
30-
31- // If characters are entered too slowly, it's probably typing and we should ignore it
32- if let lastTime = lastKeyPressTime,
33- currentTime. timeIntervalSince ( lastTime) > configuration. maximumInterCharacterTime {
34- onScan ( . failure( HIDBarcodeParserError . timedOut ( barcode: buffer) ) )
35- resetScan ( )
29+ guard shouldRecogniseAsScanKeystroke ( key) else {
30+ return
3631 }
3732
38- lastKeyPressTime = currentTime
39-
4033 let character = key. characters
4134 if configuration. terminatingStrings. contains ( character) {
4235 processScan ( )
4336 } else {
4437 guard !excludedKeys. contains ( key. keyCode) else { return }
38+ checkForTimeoutBetweenKeystrokes ( )
4539 buffer. append ( character)
4640 }
4741 }
4842
43+ private func shouldRecogniseAsScanKeystroke( _ key: UIKey ) -> Bool {
44+ guard key. characters. isNotEmpty else {
45+ // This prevents a double-trigger-pull on a Star scanner from adding an error row –
46+ // Star use this as a shortcut to switch to the software keyboard. They send keycode 174 0xAE, which is
47+ // undefined and reserved in UIKeyboardHIDUsage. The scanner doesn't send a character with the code.
48+ // There seems to be no reason to handle empty input when considering scans.
49+ return false
50+ }
51+
52+ if buffer. isEmpty && configuration. terminatingStrings. contains ( key. characters) {
53+ // We prefer to show all partial scans, but if we just get an enter with no numbers, ignoring it makes testing easier
54+ return false
55+ }
56+
57+ return true
58+ }
59+
60+ private func checkForTimeoutBetweenKeystrokes( ) {
61+ // If characters are entered too slowly, it's probably typing and we should ignore the old input.
62+ // The key we just received is still considered for adding to the buffer – we may simply reset the buffer first.
63+ let currentTime = timeProvider. now ( )
64+
65+ if let lastTime = lastKeyPressTime,
66+ currentTime. timeIntervalSince ( lastTime) > configuration. maximumInterCharacterTime {
67+ onScan ( . failure( HIDBarcodeParserError . timedOut ( barcode: buffer) ) )
68+ resetScan ( )
69+ }
70+
71+ lastKeyPressTime = currentTime
72+ }
73+
4974 private let excludedKeys : [ UIKeyboardHIDUsage ] = [
5075 . keyboardCapsLock,
5176 . keyboardF1,
@@ -138,6 +163,7 @@ final class HIDBarcodeParser {
138163 }
139164
140165 private func processScan( ) {
166+ checkForTimeoutBetweenKeystrokes ( )
141167 if buffer. count >= configuration. minimumBarcodeLength {
142168 onScan ( . success( buffer) )
143169 } else {
0 commit comments