0

How can I detect simultaneous key presses in python running in a headless session, i.e. with no display attached?

My goal is to control two motors in a car by pressing keys on the keyboard. E.g. motor 1 runs while 'a' is pressed. Motor 2 runs while 's' is pressed. If both keys are pressed, both motors run. As soon as a key is released, the respective motor stops. The Raspberrypi is not connected to a display and runs headless, i.e. I'm connecting from a Mac via SSH.

I have found several methods, but non of them works for my case where I need to detect simultaneous presses and no display is attached.

Curses and tty cannot detect simultaneous presses (example). pygame can detect simultaneous presses but does not work in a headless session (ref)

2
  • Why not define another button for when both motors should turn? Barring that have you tried anding the two key detections in an if statement? What methods have you tried and why/how have they not worked, Aug 5, 2017 at 21:10
  • The two motors are attached to left and right wheel, so in order to do smooth turns, I need to balance activity of both motors. I think quickly releasing a key to pause one motor for a moment to adjust direction of driving just a little bit would be a nice interface. However, as far as I understand curses and tty only detect when a key was pressed but not when it was released, making it impossible to implement this approach.
    – malte
    Aug 5, 2017 at 21:32

1 Answer 1

0

To the best of my limited knowledge this isn't possible right out of the box.

You can get multiple simultaneous keypresses and states only via the input event system of the Linux kernel, as far as I know. But the input system deals only with locally attached devices under normal circumstances. However, as you are connected via SSH this session is a terminal session, which is a completely different thing: it more or less only transmits simple key input and output, but doesn't act in the way the input event system works.

It is possible to have a local daemon that injects synthetic input events; for instance, some touch display drivers work this way, such as the Pimoroni HyperPixel touch LCD HAT. In you project, you would need to gather input events on the system you are sitting at, then transferring them using your own protocol to the Pi, where your daemon would inject them into the input event system.

Your Answer

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

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