1

I have a problem about determining the keyboards. Let me explain what I want.

I have a Linux Single Board Computer which has two connected keyboards(readers). Let's say one of them is keyboard_one and the other one is keyboard_two. I wrote a program which takes input from user ( it is worked as a keypad - takes password). I can take the passwords and check it is granted or denied well but:

I need to determine which password is come from keyboard_one or which one is come from keyboard_two.

So I'm looking for a python library/module that can accomplish this. In case it matters, I am using Debian and coding with Python.

5
  • Welcome. Your question is off topic, as it does not ask us to recommend software. Please read How to Ask Keyboards, mice, etc are all lumped under Human Interface Devices (HID), so google around with that phrase. And, good luck
    – Mawg
    Commented Aug 17, 2021 at 6:04
  • 1
    A welcome from me as well! You'd brought me into a conflict, Felix: I didn't want to contradict @MawgsaysreinstateMonica – but I thought a little rephrasing might help your issue. Did I understand your needs correctly? In that case I hope Mawg will forgive me, we can simply cleanup comments, and wish for some good recommendations to arrive soon. (Mawg: there's certainly a Python lib telling dev/ttyX from /dev/ttyZ, so would you agree with my rephrasing? I wouldn't even wonder much if that lib already ships inside os.* ;)
    – Izzy
    Commented Aug 17, 2021 at 15:30
  • 1
    Lolx!! I would like it think that there is always a welcome for new participants :-) It seems like a few comments were deleted, but the update to say "I'm looking for a python library/module" certainly makes this question on-topic. OP, please don't think that we think that new-guy questions are always knee-jerk & that we are anti new guys or even unclear questions. It's just a matter of "please give us info to help us to help you". Again - welcome aboard - we want to help you (ad hope that maybe you can help others) (p.s thanx @Izzy ;-)
    – Mawg
    Commented Aug 17, 2021 at 21:45
  • 1
    @MawgsaysreinstateMonica no deleted comments. If you check the post history, I simply deduced that from the concluding sentence. I might be wrong with it (needs Felix to say – I hope we didn't scare him away with our word play) – but even if, I'd say answers to this rephrased question will help him. And that's what it's about, right? ;)
    – Izzy
    Commented Aug 18, 2021 at 20:05
  • Yup, that's exactly what it is about. That and nothing else. IIRC, you can't tell on Windows - it's all just input from the HDMI. On Linux, I would expect to be able to read from /devusb0, /dev/usb1, etc, but I can't say for sure. Although my profession is developing for Linux targets, it's all embedded - and we don't any HID.
    – Mawg
    Commented Aug 19, 2021 at 6:59

1 Answer 1

1

Ok, I DuckDuckGoed that for you. See the accepted answer to How to distinguish input from different keyboards?. No library required, apparently.

If you're using Linux, the best way to distinguish between input devices is to use the Linux Event Interface. After a device's hardware-specific input is decoded, it's converted to an intermediate Linux-specific event structure and made available by reading one or more of the character devices under /dev/input/. This is completely independent of the programming language you use, by the way.

Each hardware device gets its own /dev/input/eventX device, and there are also aggregates (e.g. /dev/input/mice which represents the motion of all mice in the system). Your system may also have /dev/input/by-path and /dev/input/by-id.

There's an ioctl called EVIOCGNAME which returns the name of the device as a humanly-readable string, or you can use something like /dev/input/by-id/usb-Logitech_USB_Gaming_Mouse-mouse.

You open the device, and every time an event arrives from the input hardware, you'll get a packet of data. If you can read C, you can study the file /usr/include/linux/input.h which shows exactly how this stuff works. If you don't, you could read this question which provides all the information you need.

The good thing about the event interface is that you just find out what device you need, and you can read input from that input device only, ignoring all others. You'll also get notifications about keys, buttons and controls you normally wouldn't by just reading the ‘cooked’ character stream from a terminal: even dead keys like Shift, etc.

The bad thing is that the event interface doesn't return ‘cooked’ characters, it just uses numeric codes for keys (the codes corresponding to each key are found in the aforementioned header file — but also in the Python source of event.py. If your input device has unusual keys/buttons, you may need to experiment a bit till you get the right numbers.

Thanks, @Alexios, for the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.