0

I have written this code where I process scanned input from barcode scanner. But input here are from single line and I can't get difference between the next scanned input and the past one. Here is my code:

from evdev import InputDevice, categorize, ecodes
import select

dev = InputDevice('/dev/input/event2')
data = ""

def parse_key_to_char(val):
    return CODE_MAP_CHAR[val] if val in CODE_MAP_CHAR else ""

for event in dev.read_loop():
    if event.type == ecodes.EV_KEY:
        e = categorize(event)
        if e.keystate == e.key_up:
            print parse_key_to_char(e.keycode)

And the Output is:

0
7
5
6
7
8
1
6
4
1
2
5

While expected output is :

075678164125

It works if I am in the same terminal but won't work if I am focusing on some other terminal. I want these inputs to be captured in the background and pass them to some other function which is calling current function.

  • Seems like the problem may lie within your `parse_key_to_char()` function. – David Owens Feb 15 '18 at 07:48
  • def parse_key_to_char(val): return CODE_MAP_CHAR[val] if val in CODE_MAP_CHAR else "" – codeweiser Feb 15 '18 at 07:59
  • Don't post code in comments, edit/add it to your post if it is relevant – David Owens Feb 15 '18 at 08:10
  • 1
    It was edited but I mistakenly add code in the comment. – codeweiser Feb 15 '18 at 08:23
  • 1
    Possible duplicate of [How to print without newline or space?](https://stackoverflow.com/questions/493386/how-to-print-without-newline-or-space) Assuming you're in Python 2, I *highly* recommend the `__future__` import and using the new version. Future proofs your code *and* is a cleaner, more intuitive solution. – jpmc26 Feb 15 '18 at 19:00
  • Also, I think you mean **"output"** in most of the places where you say "input" in this question. – jpmc26 Feb 15 '18 at 19:02

1 Answers1

0

print automatically adds a new line at the end of printed text. You need to add all symbols to one string and then print it:

for event in dev.read_loop():
    if event.type == ecodes.EV_KEY:
        e = categorize(event)
        if e.keystate == e.key_up:
            text = text + parse_key_to_char(e.keycode)
print text

PS. You can add a comma after print, then there will be no new line, but a space:

for i in range (5):
    print i,

Output:

0 1 2 3 4
Psytho
  • 3,313
  • 2
  • 19
  • 27
  • read_loop is an endless loop so it won't terminate to print outside for loop. – codeweiser Feb 15 '18 at 07:59
  • How do you determine when one input ends and the other begins? – Psytho Feb 15 '18 at 08:01
  • Depending on the python version, `print` may need to be `print()` (Python 3) – David Owens Feb 15 '18 at 08:09
  • 1
    @DavidOwens Yes, but posted code has no `()` so it's not Python 3 ;) And as far as I know, Python2 is the default version on RasPi. – Psytho Feb 15 '18 at 08:11
  • @Psytho I am trying to figure out that. But since after specific input, there is a time gap I am writing a thread to clear variable and capture new variable – codeweiser Feb 15 '18 at 08:22
  • You can put single numbers in a string on the receiver side. And when after a certain time gap no further numbers come, that would mean the end of a barcode. If you use standardized barcode, they have BEGIN and END marks. – Psytho Feb 15 '18 at 08:26
  • Please don't answer duplicate questions: https://meta.stackexchange.com/q/10841/216712. – jpmc26 Feb 15 '18 at 19:05
  • @jpmc26 And how exactly am I supposed to know it is a duplicate? – Psytho Feb 17 '18 at 18:02
  • @Psytho Most problems are common and have already been asked and answered. Searching for duplicates before you answer is always a prudent step, doubly so if you recognize the problem quickly. – jpmc26 Feb 17 '18 at 23:43