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

Author Topic: Problem with IsKeyDown() allthough IsMouseButtonDown() works  (Read 4762 times)

0 Members and 1 Guest are viewing this topic.

MosteM

  • Newbie
  • *
  • Posts: 3
    • View Profile
Problem with IsKeyDown() allthough IsMouseButtonDown() works
« on: February 27, 2009, 12:56:17 am »
Like the subject allready says the first piece of code works perfectly fine, but IsKeyDown() in the second one never returns true. Both pieces are from one member function and there is no code between them which could cause problems.

Code: [Select]

if(Win->GetInput().IsMouseButtonDown(sf::Mouse::Right))
{
...
}


Code: [Select]

if(Win->GetInput().IsKeyDown(sf::Key::A))
{
...
}


I've tested it with different keys and IsMouseButtonDown(sf::Mouse::Middle) for example doesn't work either. I hope I haven't overseen something obvious and would be very glad about an answer.

P.S.: I'm quite new to SFML, but until now it's fantastic!

Astrof

  • Full Member
  • ***
  • Posts: 135
    • View Profile
Problem with IsKeyDown() allthough IsMouseButtonDown() works
« Reply #1 on: February 27, 2009, 01:48:51 am »
could you maybe show more code around the if statements?  also what system/os are you using?

MosteM

  • Newbie
  • *
  • Posts: 3
    • View Profile
Problem with IsKeyDown() allthough IsMouseButtonDown() works
« Reply #2 on: February 27, 2009, 02:20:38 am »
Of course! And thank you for the fast reply.

I'm using WinXp Sp3 32 Bit and no fancy keyboard or mouse.

This is my main loop:

Code: [Select]

while(Win->IsOpened())
{
while(Win->GetEvent(*Ereignis))
{
if(Ereignis->Type == Event::Closed)
{
Win->Close();
}
if((Ereignis->Type == Event::KeyPressed) && (Ereignis->Key.Code == Key::Escape))
{
Win->Close();
}
if((Ereignis->Type == Event::KeyPressed) && (Ereignis->Key.Code == Key::C))
{
Feld.Neu();
}
if((Ereignis->Type == Event::KeyPressed) && (Ereignis->Key.Code == Key::D))
{
DichteAusgabe = !DichteAusgabe;
}
if((Ereignis->Type == Event::KeyPressed) && (Ereignis->Key.Code == Key::V))
{
GeschwAusgabe = !GeschwAusgabe;
}
}
Ausfuehrzeit += Win->GetFrameTime();
while(Ausfuehrzeit > dt)
{
//eingabe

//physik
Manager->Update();                  //THIS IS IMPORTANT!!!

Ausfuehrzeit -= dt;
}
//grafik
Render->BeginneMalen();

if(GeschwAusgabe)
{
Manager->MaleGeschw();
}
if(DichteAusgabe)
{
Manager->MaleDichte();
}

Render->StoppeMalen();

}


The if-statement in the main loop is for a constant time step. Manager->Update(); This is where all the calculations and the input are handled. All it does is reset some values each frame then it takes care of the input and then it does the calculations. The function for the input which is shown below is called directly here.

Code: [Select]

mx = Eingabe->MausX();
my = Render->GetHoehe() - Eingabe->MausY();

if(!Win->GetInput().IsMouseButtonDown(sf::Mouse::Left) && !Win->GetInput().IsMouseButtonDown(sf::Mouse::Right))
{
mx0 = mx;
my0 = my;
return false;
}

i = (int)((Eingabe->MausX() / (skalar)Render->GetBreite()) * Feld.N + 1);
j = (int)((((skalar)Render->GetHoehe() - Eingabe->MausY()) / (skalar)Render->GetHoehe()) * Feld.N + 1);

if(i < 1 || i > (signed)Feld.N || j < 1 || j > (signed)Feld.N)
{
mx0 = mx;
my0 = my;
return false;
}

if(Win->GetInput().IsMouseButtonDown(sf::Mouse::Left))
{
Feld.u[Feld.IX(i, j)] = MAUSKRAFT * (mx-mx0);
Feld.v[Feld.IX(i, j)] = MAUSKRAFT * (my-my0);
}
if(Win->GetInput().IsMouseButtonDown(sf::Mouse::Right))
{
Feld.roh[Feld.IX(i, j)] = DICHTESTAERKE;
}
if(Win->GetInput().IsKeyDown(sf::Key::Left))                          //HERE IS THE PROBLEM
{
Feld.roh[Feld.IX(i, j)] = -DICHTESTAERKE;
}

mx0 = mx;
my0 = my;

return true;
}


The input through events in the main loop works.

If it helps I can translate the names into English.

Astrof

  • Full Member
  • ***
  • Posts: 135
    • View Profile
Problem with IsKeyDown() allthough IsMouseButtonDown() works
« Reply #3 on: February 27, 2009, 02:27:37 am »
Now I'm new to SFML too, but I'm having trouble understanding your code...

what are you trying to do? (not trying to sound mean), it seems the code will only fire if the left or right mouse button are pressed; if the mouse buttons (not middle) are not pressed, the function returns false.  are you trying to click and press a button?

Code: [Select]

   if(!Win->GetInput().IsMouseButtonDown(sf::Mouse::Left) && !Win->GetInput().IsMouseButtonDown(sf::Mouse::Right))
   {
      mx0 = mx;
      my0 = my;
      return false;//will return if left and right mouse keys are not pressed
   }


Code: [Select]

if(Win->GetInput().IsKeyDown(sf::Key::Left))                          //HERE IS THE PROBLEM
   {
      Feld.roh[Feld.IX(i, j)] = -DICHTESTAERKE;
   }

   return true;//won't get here unless mouse buttons are pressed
}

MosteM

  • Newbie
  • *
  • Posts: 3
    • View Profile
Problem with IsKeyDown() allthough IsMouseButtonDown() works
« Reply #4 on: February 27, 2009, 02:38:48 am »
OH NO!!!
Thank you so much!

I'm quite new to object orientated programming too which is probably why I just oversaw that while adding the new keyboard functionality to the function. As you might have seen, the function is quite messy because I added some new functionalities and haven't cleaned up until now.

Thanks again!

Astrof

  • Full Member
  • ***
  • Posts: 135
    • View Profile
Problem with IsKeyDown() allthough IsMouseButtonDown() works
« Reply #5 on: February 27, 2009, 02:52:04 am »
It's all good, that's what the forums are for ^_^.  I'm fairly new to SFML and C++ too so I know what its like to be frustrated.  

Anyway, glad the problem is fixed and good luck on your project!

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Problem with IsKeyDown() allthough IsMouseButtonDown() works
« Reply #6 on: February 27, 2009, 11:20:05 am »
Get used to a debugger or at least print out some debug messages. Leads you mostly very fast to the problem. ;)

 

anything