← Back to C++ Course | Chapter 18: Graphics | Lesson 5 of 7

C++ में Mouse और Keyboard Input

Mouse और keyboard input आपके program को keys और clicks पर react करने देता है, आपके controller पर react करते एक game की तरह। Program हर frame check करता है कि आपने क्या किया।
Syntax
cpp
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key_Name)) {
  // key is held down
}

if (event.type == sf::Event::KeyPressed) {
  // key was pressed once
}

sf::Keyboard के साथ Keyboard Input

sf::Keyboard::isKeyPressed(key) event queue से independent, आप इसे call करते वक्त एक specific key की current, real-time state check करता है -- "held होने पर right move करें" जैसी continuous actions के लिए ideal, क्योंकि इसे key के अभी state बदलने की ज़रूरत नहीं होती।

उदाहरण: Keyboard Input with sf::Keyboard

cpp
#include <SFML/Window.hpp>

int main() {
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
		// move right while the key is held
	}
	return 0;
}

Keyboard Events Handle करना

Event loop (KeyPressed/KeyReleased) के जरिए deliver होने वाले keyboard events हर press या release पर exactly एक बार fire होते हैं, held रहते हुए continuously नहीं -- यह इन्हें discrete, one-shot actions के लिए सही choice बनाता है जैसे "jump" या "open menu", जहाँ आप नहीं चाहते कि key नीचे रहने पर हर frame action repeat हो।

उदाहरण: Handling Keyboard Events

cpp
#include <SFML/Graphics.hpp>

int main() {
	sf::RenderWindow window(sf::VideoMode(400, 300), "Events");
	sf::Event event;
	while (window.pollEvent(event)) {
		if (event.type == sf::Event::KeyPressed)
			; // fires exactly once per press, good for "jump"
	}
	return 0;
}

Mouse Position Tracking

sf::Mouse::getPosition() cursor के current pixel coordinates return करता है, या तो पूरी screen के relative, या जब एक window reference pass की जाए, उस window के client area के relative -- यह जानने के लिए useful कि player screen पर exactly कहाँ point कर रहा है।

उदाहरण: Mouse Position Tracking

cpp
#include <SFML/Window.hpp>

int main() {
	sf::Vector2i screenPos = sf::Mouse::getPosition();
	return 0;
}

Mouse Click Events

Keyboard state की तरह, sf::Mouse::isButtonPressed() report करता है कि आप पूछते वक्त एक mouse button currently held down है या नहीं, जबकि event loop का MouseButtonPressed event एक single click report करता है -- जो भी आपकी continuous या one-shot behavior की ज़रूरत से match करे वह चुनें।

उदाहरण: Mouse Click Events

cpp
#include <SFML/Window.hpp>

int main() {
	if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
		// continuously true while held down
	}
	return 0;
}

Collision और Selection

getGlobalBounds() world coordinates में एक shape का bounding rectangle return करता है, और इस पर .contains(mousePosition) call करना बताता है कि cursor currently उस shape के ऊपर है या नहीं -- clickable buttons या drag-and-select interactions के लिए standard building block।

उदाहरण: Collision and Selection

cpp
#include <SFML/Graphics.hpp>

int main() {
	sf::CircleShape shape(30.f);
	sf::Vector2f mousePos(35.f, 35.f);
	bool hovered = shape.getGlobalBounds().contains(mousePos);
	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. sf::Keyboard::isKeyPressed को loop के बाहर सिर्फ एक बार check करना, इसलिए key press बाद में कभी notice नहीं होता।
  2. यह भूल जाना कि mouse coordinates screen के नहीं window के relative pixels में हैं।
  3. key events handle करना लेकिन sf::Event::Closed event नहीं, इसलिए window close नहीं हो सकती।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

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.