Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Escape key event triggered while holding left mouse button.  (Read 2859 times)

0 Members and 2 Guests are viewing this topic.

Xunxen

  • Newbie
  • *
  • Posts: 2
    • View Profile
I'm using sfml 2.0 on mac, and I'm having this issue while holding the mouse button down where the escape key event is being triggered seemingly randomly. This only seems to be happening while my mouse button is down while moving the cursor, and happens around 1-30? seconds after clicking. it seems like the escape key is the only key that's being "pressed".
the code I'm using:
Code: [Select]

#include<SFML/Graphics.hpp>
#include<iostream>

int main(){

float x=400,y=300;
int r=5;
double Vx=2, Vy=2,g=1.5;
bool show=true;

struct throwable{

double x,y;
bool doThrow;

};

throwable t;
t.doThrow=false;

sf::Shape c=sf::Shape::Circle(0,0,r,sf::Color::White);
c.SetPosition(x,y);
sf::RenderWindow w(sf::VideoMode(800,600),"Bounce");
w.SetFramerateLimit(60);

while(w.IsOpened()){

sf::Event e;
while(w.PollEvent(e)){

if(e.Key.Code==sf::Key::Escape){

w.Close();
std::cout << "Escape key pressed." << std::endl;

}
if(e.Type==sf::Event::Closed){

w.Close();
std::cout << "Window closed." << std::endl;


}

}


const sf::Input& i=w.GetInput();
if(i.IsKeyDown(sf::Key::A)) std::cout << "A key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::B)) std::cout << "B key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::C)) std::cout << "C key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::D)) std::cout << "D key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::E)) std::cout << "E key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::F)) std::cout << "F key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::G)) std::cout << "G key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::H)) std::cout << "H key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::I)) std::cout << "I key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::J)) std::cout << "J key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::K)) std::cout << "K key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::L)) std::cout << "L key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::M)) std::cout << "M key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::N)) std::cout << "N key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::O)) std::cout << "O key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::P)) std::cout << "P key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::Q)) std::cout << "Q key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::R)) std::cout << "R key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::S)) std::cout << "S key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::T)) std::cout << "T key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::U)) std::cout << "U key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::V)) std::cout << "V key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::W)) std::cout << "W key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::X)) std::cout << "X key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::Y)) std::cout << "Y key pressed." << std::endl;
if(i.IsKeyDown(sf::Key::Z)) std::cout << "Z key pressed." << std::endl;

if(i.IsMouseButtonDown(sf::Mouse::Left)){

Vx=0;
Vy=0;
x=t.x=i.GetMouseX();
y=t.y=i.GetMouseY();
t.doThrow=true;
if(show){
std::cout << "Mouse button pressed." << std::endl;
show=false;
}

}
if(t.doThrow&&!i.IsMouseButtonDown(sf::Mouse::Left)){

t.doThrow=false;
Vx=(i.GetMouseX()-t.x)/2;
Vy=(i.GetMouseY()-t.y)/2;
std::cout << "Mouse button released." << std::endl;
show=true;
}

w.Clear();
w.Draw(c);
w.Display();

if(x+Vx<=r) Vx=-0.7*Vx;
if(y+Vy<=r) Vy=-0.7*Vy;
if(x+Vx>=w.GetWidth()-r) Vx=-0.7*Vx;
if(y+Vy>=w.GetHeight()-r) Vy=-0.7*Vy;
c.SetPosition(x,y);
x+=Vx+(-0.2*Vx);
y+=(Vy+=g);


}

}


Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Escape key event triggered while holding left mouse button.
« Reply #1 on: July 02, 2011, 10:38:45 am »
You have to check the type of the event (ie event.Type) before testing its data (ie event.Key.Code, etc...). See http://www.sfml-dev.org/tutorials/1.6/window-events.php
SFML / OS X developer

Xunxen

  • Newbie
  • *
  • Posts: 2
    • View Profile
Escape key event triggered while holding left mouse button.
« Reply #2 on: July 02, 2011, 03:12:51 pm »
Oh, wow. I didn't even think of that, but that fixes it. thank you.

 

anything