I am doveloping a game brain champ. This is its version 3. In this version I want to use mouse in addition with keyboard for input. I have successfully tested its standalone keyboard version as well as mouse version. I have used int86() function for mouse with 0x33 interrupt number. Also I have used getch() function for keyboard input. But my main problem is I m not able to use mouse and keyboard simultaneously. As when I do so getch() holds the program and thus I can't get mouse input simultaneously. Please help me out. I am working on turbo c++ 3.0. I am an experienced programmer. Please give me sample code so that I can use it accordingly. Thanks in advance.

Dani AI

Generated

Short answer: run a single non‑blocking event loop that polls both devices each frame, and hide the mouse while you rewrite the screen. was right to point at kbhit() — avoid letting getch() block the loop — and ’s header already gives you the show/hide wrappers you need for the mouse cursor.

Practical approach (steps to apply)

  1. Initialize the mouse driver (your existing int‑33h code) and keep a tiny in‑program key queue (ring buffer).
  2. In the main loop: poll keyboard (use kbhit() and read characters into your queue), poll the mouse (int 33h to get x/y and buttons), then handle one or more input events and draw a new frame. Do not call a blocking getch() except when you actually want to pause.
  3. To update the screen safely, hide the mouse cursor, perform clearviewport() / blit your backbuffer to the screen, then show the cursor again. That sequence prevents the mouse driver from restoring an older saved background and leaving the pixel region under the pointer uncleared — see @paul.stevin’s MouseOff/MouseOn helpers for that hide/show.

Example structure (conceptual C, adapt to your BGI/int86 wrappers):

/* conceptual structure */
char keyq[32];
int qh=0, qt=0;

void enqueue(int c){ if ((qt+1)%32!=qh) keyq[qt]=c, qt=(qt+1)%32; }
int dequeue(){ if (qh==qt) return -1; int c=keyq[qh]; qh=(qh+1)%32; return c; }

for(;;){
  while(kbhit()) enqueue(getch());        /* poll keyboard into queue */
  int k = dequeue();                      /* process at your pace */
  if (k!=-1) handle_key(k);
  poll_mouse_and_handle();                /* int 33h read */
  MouseHide();                            /* hide pointer (driver call) */
  draw_backbuffer_to_screen();            /* clearviewport/putimage/etc. */
  MouseShow();                            /* show pointer */
  delay(16);                              /* frame pacing (~60Hz) */
}

Notes and troubleshooting

  • Don’t use fflush(stdin) — it’s undefined for input.
  • Double‑buffering (draw to an offscreen image then blit) reduces flicker and keeps the hide/show window small.
  • If testing on modern machines, run Turbo C++ in DOSBox or a real DOS environment so the int‑33h mouse behavior is correct.

Recommended Answers

All 8 Replies

Before calling getch() find out if anything is in the keyboard buffer kbhit() from conio.h will tell you that. If nothing there then you can check for mouse input.

Thanks for your reply ancient dragon. I understand the whole concept but there is a small problem if I hit keyboard more than one time then getch() just take one input at one time. So buffer has input still after the call to getch(). How can I clear this buffer() after single call to getch(). Fflush(stdin) isn't working.

fflush is not supposed to work with stdin -- it's only for output streams like stdout. You can write your own fflush like this

void flushKeyboard()
{
   while( kbhit() )
       getch();
}

U r such a beautiful dragon. Thank you so much. :-).
But as u knw problems never end for a programmer. There is another minor problem. Whenever I use clearviewport() to clear the screen the portion of screen beneath the mouse cursor remains there uncleared. What should I do to clear that portion.

I have no idea -- have not used Turbo C in 30 years.

Ok thanks. I will make out that thing on my own, thank you so much.

helo sanyam.mishra this is header file *.h include most func you need with turbo C++
you must manipulate registry value with ("reg., init86().....") included from <dos.h>
If you need any help

E- mail: 51284191@orange.tn
#include <dos.h>
#include <stddef.h>


   typedef enum Boolean {false,true}boolean;
   union REGS reg;
   int etat();
   int X();
   int Y();
   void MouseInit();
   void MouseOn();
   void MouseOff();
   int Left();
   int Right();
   int MouseIn();
   void curseur(int x,int y);
   void MouseInit()
   {
      reg.x.ax=0x00;
      int86(0x33,®,®);
   }
   void MouseOn()
   {
     reg.x.ax=0x01;
     int86(0x33,®,®);
   }
   void MouseOff()
   {
      reg.x.ax=0x02;
      int86(0x33,®,®);
   }
   int X()
   {
      reg.x.ax=0x03;
      int86(0x33,®,®);
      return(reg.x.cx);
   }
   int Y()
   {
     reg.x.ax=0x03;
     int86(0x33,®,®);
     return(reg.x.dx);
   }
   int Left()
   {
      reg.x.ax=0x03;
      int86(0x33,®,®);
      return((reg.x.bx==1));
   }
   int Right()
   {
      reg.x.ax=03;
      int86(0x33,®,®);
      return((reg.x.bx==2));
   }
   int MouseIn(int x1,int x2,int y1,int y2)
   {
      reg.x.ax=0x03;
      int86(0x33,®,®);
      reg.x.bx=2;
      return ((X()>=x1)&(X()<=x2)&(Y()<=y2)&(Y()>=y1));
   }
   int etat(void)
   {
     reg.x.ax=0x03;
     int86(0x33,®,®);
     return(reg.x.bx);
   }
   ------
   void cursor_pointer(int x,int y)
{
while(!kbhit())
{
setcolor(0);
outtextxy(x,y,"_");
delay(150);
setcolor(9);
outtextxy(x,y,"_");
delay(150);
}
}

-------------//good luck//

Thanks paul but I had already figured this put. But anyway I will save this for my future reference. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.