0

For my DIY access control system, I'm running Debian on a Raspberry Pi attached to a MagStripe card reader. When a card is swiped, the reader writes the data from the magnetic stripe over the USB port by acting like a keyboard. For example, plug it into your computer, open a text editor, and swipe a card and you'll see a string of data printed out like you typed it with your keyboard.

Simple connection diagram:

Client <--ssh--> Host + card reader

The only problem is that I'm running my Python script over ssh, which doesn't directly hear the keyboard input.

How can I get Python to listen to the keyboard events coming from the MagStripe reader? (do I need to use a keylogger? PyGame's keyboard bindings?)

Thanks for the help!

hao_maike
  • 2,546
  • 5
  • 23
  • 30

2 Answers2

2

On Linux, USB keyboards can be accessed via /dev/input.

See: format of /dev/input/event*? - the answers to that question include two different Python modules for getting events, and links to more documentation.

To translate from key codes to ASCII, see How can I translate Linux keycodes from /dev/input/event* to ASCII in Perl?

You will probably need to be root, or change the permissions on /dev/input.

Community
  • 1
  • 1
user9876
  • 10,362
  • 6
  • 38
  • 64
0

It may be worth dividing the program into a two pieces: one as a service that starts at boot time on the raspberry pi which will get its stdin from your keyboard device(the reader) by default, and another that provides remote access functionality over ssh. This way you can avoid having to deal with devices in /dev directly.

clampfferj
  • 31
  • 1