How to build a simple alternative input device
This page describes how to build a simple alternative input device
that connects to the serial port. This device can have three buttons
and stay simple; more buttons are possible, but they would make things
more complicated. The hardware is as simple as three light switches.
The software is a little bit more complicated, but not much. If you
use a PC running Windows, then you can use the software that I've
written (see below). If you use another operating system, then you'll
have to write your own software (please let me know if you do).
I use this for a footswitch. When I work on OSX or GNU/Linux, I connect my footswitch (same hardware, different wiring) to the RJ11 plug on a Kinesis Ergo keyboard, so I haven't written software for those platforms. It's fairly easy to reverse engineer the pin functions on this plug and hook up whatever you want.
Contents:
Hardware
How to connect to the serial (RS232) port
It is fairly easy to do simple things with the serial (RS232) port on
a PC (or Mac, or whatever other computer you have with an RS232 port).
The serial port is conventionally used to communicate between a
computer and modem. There's one pin for the computer to send data to
the modem, and one pin for the modem to send data to the computer.
The other pins are used for control functions, for example, so the
computer can say to the modem "I have some data to send you". It is
very easy to use these control pins for another purpose, which is what
we're going to do. It is more complicated to use the data
transmission pins. Here is a listing in description of all the pins
in a standard RS232 serial port.
D-Type-25 Pin No. | D-Type-9 Pin No. | Abbreviation | Full Name |
Pin 2 | Pin 3 | TD | Transmit Data |
Pin 3 | Pin 2 | RD | Receive Data |
Pin 4 | Pin 7 | RTS | Request To Send |
Pin 5 | Pin 8 | CTS | Clear To Send |
Pin 6 | Pin 6 | DSR | Data Set Ready |
Pin 7 | Pin 5 | SG | Signal Ground |
Pin 8 | Pin 1 | CD | Carrier Detect |
Pin 20 | Pin 4 | DTR | Data Terminal Ready |
Pin 22 | Pin 9 | RI | Ring Indicator |
Table 1 : D Type 9 Pin and D Type 25 Pin Connectors
  | Abbreviation | Full Name | Function |
  |    | TD | Transmit Data | Serial Data Output (TXD) |
  |    | RD | Receive Data | Serial Data Input (RXD) |
  |    | CTS | Clear to Send | This line indicates that the Modem is ready to exchange data. |
  |    | DCD | Data Carrier Detect | When the modem detects a "Carrier" from the modem at the other end of the phone line, this Line becomes active. |
  |    | DSR | Data Set Ready | This tells the UART that the modem is ready to establish a link. |
  |    | DTR | Data Terminal Ready | This is the opposite to DSR. This tells the Modem that the UART is ready to link. |
  |    | RTS | Request To Send | This line informs the Modem that the UART is ready to exchange data. |
  |    | RI | Ring Indicator | Goes active when modem detects a ringing signal from the PSTN. |
Figure 1: Pin positions for a 9 pin serial port
The DTR and RTS pins are output control pins: from the computer to
the serial device. The CTS, DCD, and DSR pins are input control pins:
from the serial port device to the computer. The RI pin is also an
input control pin, but it seems to behave a little bit differently on
my computer.
The basic idea is to connect the computer's output control pins to its
input control pins, with a simple physical switch in the middle. Then
we have some software that monitors the state of the control pins and
generates keyboard events when the state changes, that is, when the
physical switch is opened or closed. Thus, the hardware is as
simple as a light switch.
Since we have three input pins, it is easy to make a device with three
buttons. Those of you with a computer science background will know
that, in theory, with three input pins we could make a device with
eight buttons. The practical problem is that control pin state
changes do not occur simultaneously in the real world, and even if
they did, the software seems to recognize them consecutively rather
than simultaneously. This can probably be worked around with some
cleverness and complication, but I'm going to stick with three buttons
for now. Let me know if you have a solution to this practical
problem.
Good references on serial ports
Building the device
Any three switches will suffice for this kind of device. You can
build whatever strikes your fancy. I went down to Active Surplus and
bought an old foot control from a dictaphone machine. It has play,
rewind and fast-forward buttons. I just cut the connector off the end
of the cord and put on a nine pin serial connector. The total cost
was about $11.
This old dictaphone foot control is wired in such a way that only one
of the buttons will work at a time. You may or may not want your
alternative input device to behave in this way. It's easier to make
your device so that the buttons do not interfere with each other, but
it's not too hard to make it like my foot control. The main
difference is that my foot control uses which is with three-terminals
instead of switches with two terminals. The three-terminal switches
have one input terminal and two output terminals: the switch just
toggles between the two output terminals. Figure 2 shows what the
wiring looks like. The circles represent pins on the serial port and
the triangles represent the three-terminal switches in the foot
control.
Figure 2: wiring of my foot control
Software
Download
This software works for Windows 98 and up (see below). It is written
in Visual Basic.
General Overview
This is not software that you will use directly very often.
Typically, you will configure it once and just put it in your Startup
folder so that it runs everytime your computer boots. Then you will
just use your alternative input device and forget about the software.
Because of this, all of the options can be set from the command line,
so that you don't have to set them in the graphical user interface
each time you start your computer.
Command Line
This program can be totally configured (and re-configured) from the
command line. The options are:
- General Configuration:
- -port=[none|com1|com2|com3|com4]
- -dtr
- -rts
- -minimize
- (Note that the -port=comX option must be set before -dtr and -rts.)
- DSR Input Signal:
- -dsrMode=[emulateKey|sendKeys|generateKeys]
- -dsrEmulateKey={vbKeyCode}
- -dsrSendKeys={VB SendKeys syntax}
- -dsrGenerateKeys={disjunctive normal form of vbKeyCodes; "+" and ","}
- CTS Input Signal:
- -ctsMode=[emulateKey|sendKeys|generateKeys]
- -ctsEmulateKey={vbKeyCode}
- -ctsSendKeys={VB SendKeys syntax}
- -ctsGenerateKeys={disjunctive normal form of vbKeyCodes; "+" and ","}
- CD Input Signal:
- -cdMode=[emulateKey|sendKeys|generateKeys]
- -cdEmulateKey={vbKeyCode}
- -cdSendKeys={VB SendKeys syntax}
- -cdGenerateKeys={disjunctive normal form of vbKeyCodes; "+" and ","}
Note that all options are case sensitive. Also note that you cannot
have spaces in the SendKeys value (or any other value) because the
command line scanner will get confused (yes, this is lame).
When the program starts, it looks for a previous instance of itself.
If a previous instance is found, the current instance initiates a
dynamic data exchange (DDE) link with the previous instance and sends
it the current command line. The previous instance then resets itself
according to that command line. The current instance terminates.
System Compatibility
At the core, this software generates Windows input events with the
SendInput function of the Win32 API. The usage here is based on the
documentation available at:
vbapi.com
SendInput works on the following Microsoft operating systems:
- Windows 95: Not Supported.
- Windows 98: Supported.
- Windows NT: Requires Windows NT 4.0 with Service Pack 3 (SP3) or later.
- Windows 2000: Supported.
- Windows CE: Requires Windows CE 2.0 or later.
Windows 95 has the keybd_event function, which is supported
but obsolete on Windows 98: SendInput is now the preferred
function. It would not be too hard to modify this software to use the
keybd_event function so that it would be compatible with
Windows 95.
I have successfully run the build from this webpage on Win98, 2k, and XP.
Keyboard Semantics
Buttons on a keyboard and mouse do two things: they go down and they
come up. Sometimes both motions have meaning and sometimes only one
of the motions has meaning. For example, the shift keys register on
both the down motion and the up motion; the caps lock key only
registers on the down motion; and the right mouse button usually only
registers on the up motion.