'Running keyboard module in python is returning weird characters

I am running a python script to capture keyboard input in order to read a bar code reader scanner on a debian 9.0 system, the bar code reader is supposed to work as a usb keyboard.

import keyboard
import sys

def main():
    """
    Installs keyboard event listener and waits until "enter" key is pressed.
    """
    try:
        recorded = keyboard.get_typed_strings(keyboard.record('enter'))
        sys.stdout.write(str(list(recorded)[0]).strip())
        return 0
    except Exception as e:
        print(e)
        sys.stderr.write("An unexpected error occurred!")
        return 1


if __name__ == "__main__":
    sys.exit(main())

The script crashes with this error 'utf-8' codec can't decode byte 0x89 in position 319: invalid start byte even before scanning or even when the bar code scanner is not connected to computer.

I tried the bar code reader with a text editor and it is working fine with each scan, it writes down the data in the file.

I tested this script on a raspberry pi and it is working as expected as well.

It looks like the keyboard module in python is capturing keyboard interrupt from some where with lots of strange data, but not sure what's going on.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source