C में Mouse Input
In this page:
union REGS in, out;
in.x.ax = function_code; // 0 = init, 1 = show, 3 = position/buttons
int86(0x33, &in, &out); // call the mouse interrupt
// results in out.x.ax, out.x.bx, out.x.cx, out.x.dx
Mouse Support Detect करना
classic DOS-era BGI setup पर, mouse किसी modern event API से नहीं बल्कि low-level interrupts से access होता है: एक union REGS structure populate करना और appropriate function number के साथ interrupt 0x33 issue करना mouse driver को initialize और इससे communicate करता है।
उदाहरण: Detecting Mouse Support
#include <dos.h>
int main() {
union REGS in, out;
in.x.ax = 0;
int86(0x33, &in, &out);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Mouse Coordinates पाना
'get position' (operation value 3) के लिए interrupt function code से mouse status request करना interrupt के output registers में cursor के current X और Y screen coordinates return करता है, जिन्हें आपका program फिर उसी union REGS structure से पढ़ता है।
उदाहरण: Getting Mouse Coordinates
#include <dos.h>
// Program execution starts in main()
int main() {
union REGS in, out;
in.x.ax = 3;
int86(0x33, &in, &out);
// Declare x and set it to out.x.cx
int x = out.x.cx;
// Declare y and set it to out.x.dx
int y = out.x.dx;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Mouse Clicks Handle करना
Mouse button state status call के बाद interrupt के output register में एक value से report होती है: 1 की value आमतौर पर left button pressed indicate करती है और 2 right button, आपको उसके आधार पर अपने program का behavior branch करने देते हुए कि कौन सा button (अगर कोई) दबा है।
उदाहरण: Handling Mouse Clicks
#include <dos.h>
int main() {
union REGS in, out;
in.x.ax = 3;
int86(0x33, &in, &out);
if (out.x.bx == 1) {
// left button pressed
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Interactive Buttons बनाना
एक clickable button बनाने के लिए, आप screen coordinates में एक rectangular region define करते हैं जो उस button की bounds represent करता है, फिर हर detected click पर report की गई mouse coordinates को उस rectangle के edges से compare करके decide करते हैं कि click इसके अंदर landed या नहीं।
उदाहरण: Creating Interactive Buttons
#include <dos.h>
int main() {
union REGS in, out;
in.x.ax = 3;
int86(0x33, &in, &out);
int x = out.x.cx, y = out.x.dx;
if (x >= 50 && x <= 150 && y >= 50 && y <= 100) {
// click inside button bounds
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Mouse Cursor Hide करना
आपके program के exit होने से पहले (या जब भी आप cursor visually hidden चाहें), operation value 2 के साथ mouse interrupt call करना mouse cursor hide करता है, जो मायने रखता है क्योंकि इसे visible छोड़ने के दौरान आपका program अपना graphics mode tear down करे तो screen पर एक stray cursor artifact रह सकता है।
उदाहरण: Hiding the Mouse Cursor
#include <dos.h>
int main() {
union REGS in, out;
in.x.ax = 2;
int86(0x33, &in, &out);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- यह check किए बिना mouse functions इस्तेमाल करना कि graphics library उन्हें support करती है, जैसे
winbgim। - यह check किए बिना mouse coordinates पढ़ना कि actually एक click हुआ।
- यह भूल जाना कि mouse coordinates drawing जैसे same top-left pixel system में हैं।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: