Clear questions and runnable code
get the best and fastest answer
Re: Morse input from keyboardby afoken (Canon)
|on Feb 11, 2020 at 20:53 UTC||Need Help??|
Some other ways of reading a single key
Legacy and USB RS232 serial ports
This is a very generic solution. Most operating systems provide some way to read at least one of the status bits of the serial port. You can read back at least one of (D)CD, DSR, CTS, and RI. You can usually set DTR and/or RTS. Often, DTR and/or RTS are already set by the operating system. In idle, DTR and RTS are negative relative to GND, and unconnected inputs are treated like negative inputs. Opening the serial port usually sets DTR and/or RTS to positive, if not, do so manually. Connect a button between DTR or RTS and one of the inputs (DCD, DSR, CTS, RI). When the button is pressed, the respective input reads as positive.
In the unlikely case that only bare read and write are available, you can still use the serial port. Connect the button between TxD and RxD, disable receive buffering as much as possible, and send out any data at the highest possible speed. As long as the button is pressed, you will see incoming data. The trick here is that humans need several milliseconds to press and release a button, during that time, several bytes travel from TxD to RxD.
This trick should work with all serial RS232 ports found on a PC, either build-in or USB.
Legacy PC joystick input
Connect a button between GND and one of the four button inputs. When running DOS, you simply read port 0x0201 and look at bits 4 to 7 to read the button inputs. Windows should support that interface, too, but I have no idea how to read it. I'm guessing that there is some device-independant DirectX API. Linux shold support that interface, too, using the event system (I guess).
USB adapter for legacy PC joystick
The same idea, but a different Windows driver. No DOS support. Windows and maybe also Linux should treat it like a legacy joystick interface.
Legacy Parallel port
You probably need to get port-level access on Windows and Linux to read the status bits.
USB parallel port adapters may be even harder to work with.
If you use a Raspi, have a look at the work of stevieb, especially RPi::Pin, and https://pinout.xyz/. Just connect the button between GND and a unused GPIO line, then configure the pin as input with a pull-up. The pin will read as 0 while the button is pressed, else 1.
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)