I am trying to get mouse and keyboard input simultaneously using Turbo-C++.
I want to use two functions. The first function would be the main
function which may take keyboard inputs or call the delay
function from dos.h
to temporarily stop the normal functioning of main()
.
The second function processes mouse input:
void mouse()
{
union REGS in,out;
in.x.ax=1; // Show mouse pointer
int86(0x33,&in,&out); //interrupt call
while(1)
{
//print the location whenever it changes (not required)
}
}
If I understand the question correctly, you are writing a program that will run under 16-bit MS-DOS, inside DosBox. That program is expected to respond to both mouse and keyboard input. It might also want to perform some computation while waiting for input.
The simplest and most straightforward solution is to use polling, meaning you sit in a loop, asking both the keyboard and mouse if they have new input to respond to. In pseudocode:
while (true) {
while (keyboard_has_activity()) {
process_keyboard_input();
}
while (mouse_has_activity()) {
process_mouse_input();
}
do_other_computation_for_a_short_time();
}
How to write keyboard_has_activity
? You mentioned kbhit
from conio.h
in a comment; that should work. There are also lower-level interrupts you could use (although I don't know of any reason not to use kbhit
).
Note: process_keyboard_input
should only read as many characters as are currently available. If your program involves waiting for the user to press Enter, then you will have to store the typed characters in (say) an array until you actually see the Enter key, and then process all of the input at once.
How to write mouse_has_activity
? To my knowledge, you have to use INT 0x33 routines to query the current mouse status and compare that to what it was on the last call.
How to write do_other_computation_for_a_short_time
? You'll have to design some sort of time bound into the algorithm. For example, if you are writing a chess engine, it would check the clock after each (say) board evaluation and stop when necessary so you can poll for input. When the algorithm stops, it must remember in a data structure where it was so it can later resume from that point. One common strategy is to maintain a worklist: a list of all the parts of the computation that still need to be done. This is a form of cooperative multitasking.
In any case, you want the time bound to be no more than about 10ms, since otherwise you might miss mouse clicks (user could press and release the button before you notice it).
This solution is simple but not necessarily the most efficient, and potentially unreliable due to the aforementioned problem with missing mouse clicks if the time bound is too long.
The other way to do it is to hook into the low-level keyboard and mouse interrupts. This way, you install code that only runs when an input device is touched, which is more efficient. However, it is also much harder to get right, for a variety of reasons, including that you have little control over what one piece of code is doing when it gets interrupted.
In this environment, it's impractical to try to organize your program as two threads (mouse and keyboard) with two functions running simultaneously. DOS has no threading facilities, and I'm not aware of any way to add them without basically rewriting the OS. Instead, you'll need to do everything from one control thread.