Reputation: 21
I need to detect, in a python console (text mode) program, when the Ctrl key is pressed, without another key at the same time. I tried with getch
from curses library and stdin, but it waits for any key but not Ctrl or Alt. I found information in stackoverflow but always referred to Windows/event environment, or pressed simultaneously with another key (Ctrl + C for example) but this is not my case. I have found in some forum that's no possible, but I can believe it.
Upvotes: 2
Views: 1073
Reputation: 15877
This answer is Linux-centric, seeing your mention of Raspbian console mode implies a Debian GNU/Linux console system.
If you're really talking about the console, this is possible albeit very hacky. Curses has a raw mode to read most keys, but Control is a modifier key, so won't show up that way. The method I can think of is to read the input device, much like X would. Use lsinput
to find which device is your keyboard. As input-events demonstrates, you can see events there while they are also processed elsewhere. Among the downsides are that you won't know if the input was actually going to you (unless you track virtual console switching, job status etc) and you need access to the device, as that property implies it might be sensitive data such as a password being entered to login on another virtual console.
It might be simpler to remap what the control key itself sends using loadkeys
, thus changing it from a modifier to a detectable key. It will still retain its lower level protocol properties (in USB HID boot protocol keyboard, for instance, it will have a dedicated bit rather than use one of typically only 6 slots for pressed keys).
Either way, this is not easy or portable, and won't work at all over terminal environments such as an ssh session.
Upvotes: 1