0

I need to grab input from barcode scanner, which works exactly as keyboard, only it "types" a lot faster. There are no delimiters, no line endings. Virtually no way of comparing scalar values - whatever's been entered by a user might look exactly as a barcode.

I guess it is possible with Rx, since barcode scans a lot faster than any user can type.

How can I create an Observable from document.keypress event that distinguishes user input from barcode scanner's? I'm guessing it should somehow buffer/window values whenever there's a "burst" of keypresses and then a pause between.

Doing this, still not helping:

Rx.Observable.fromEvent(document, 'keypress')
                .bufferWithTime(1500)
                .filter((x)=> _.isNotEmpty(x) && x.length > 5)

It just grabs whatever was typed every one and a half seconds. Can you guys help me to wrap my head around this thing?

iLemming
  • 34,477
  • 60
  • 195
  • 309

2 Answers2

1

I think what you need is timestamp operator. Then you can filter on how fast keypresses were recorded. https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/timestamp.md

Frederik Krautwald
  • 1,782
  • 23
  • 32
1

In the current version of your code, you could also use timeInterval to avoid manipulating the timestamp yourself: https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/timeinterval.md, http://reactivex.io/documentation/operators/timeinterval.html

However, a better idea IMO here is really to use the buffer operator with closing selector. Your closing selector could use the debounce operator. Cf. https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/debounce.md

Basically you would close the buffer when there is no further keys emitted after Xms (something like source.buffer(function(){return source.debounce(Xms)})). So you would have an observable of arrays (buffers). And you should carefully choose the debounce timeout so that the array size is 1 when it is a user keypress, and >1 when it is scanner inputs. My normal typing speed is 247 keypress per min for example.

Example of use of the buffer operator with closing selector : Smarter buffers

Community
  • 1
  • 1
user3743222
  • 18,345
  • 5
  • 69
  • 75
  • Sorry, I'm still struggling to understand how to use `.buffer` with closing selector. Could you please demonstrate that with a code snippet. I'd appreciate that greatly. – iLemming Nov 20 '15 at 18:55
  • ? I linked the question you yourself already asked about a similar subject (Smarter buffers). Take the code there, and replace the appropriate line with `source.buffer(function(){return source.debounce(Xms)})`. What was it precisely that you still don't understand about `buffer`? Here is an extra visual resource > http://reactivex.io/documentation/operators/buffer.html. Go down to the operators with Rxjs, there is a drawing specific to the version of `buffer` with closing selector. Generally speaking, it is worth trying to have a good understanding you are given in addition to pasting them. – user3743222 Nov 21 '15 at 00:58