C++ में Mouse और Keyboard Input
In this page:
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
#include <SFML/Window.hpp>
int main() {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
// move right while the key is held
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <SFML/Window.hpp>
int main() {
sf::Vector2i screenPos = sf::Mouse::getPosition();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <SFML/Window.hpp>
int main() {
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
// continuously true while held down
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
sf::Keyboard::isKeyPressedको loop के बाहर सिर्फ एक बार check करना, इसलिए key press बाद में कभी notice नहीं होता।- यह भूल जाना कि mouse coordinates screen के नहीं window के relative pixels में हैं।
- key events handle करना लेकिन
sf::Event::Closedevent नहीं, इसलिए window close नहीं हो सकती।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: