← Back to C Course | Chapter 13: Graphics | Lesson 8 of 10

C में Mouse Input

Mouse input आपके program को यह react करने देता है कि आप mouse कहाँ move और click करते हैं, आपके हाथ पर react करते एक game की तरह। आप इसे buttons बनाने के लिए इस्तेमाल कर सकते हैं।
Syntax
c
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

c
#include <dos.h>
int main() {
	union REGS in, out;
	in.x.ax = 0;
	int86(0x33, &in, &out);
	return 0;
}

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

c
#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;
}

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

c
#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;
}

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

c
#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;
}

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

c
#include <dos.h>
int main() {
	union REGS in, out;
	in.x.ax = 2;
	int86(0x33, &in, &out);
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह check किए बिना mouse functions इस्तेमाल करना कि graphics library उन्हें support करती है, जैसे winbgim।
  2. यह check किए बिना mouse coordinates पढ़ना कि actually एक click हुआ।
  3. यह भूल जाना कि mouse coordinates drawing जैसे same top-left pixel system में हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.