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

Author Topic: KeyPressed not working but KeyReleased is  (Read 2507 times)

0 Members and 1 Guest are viewing this topic.

alan2here

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
KeyPressed not working but KeyReleased is
« on: April 28, 2010, 07:18:31 pm »
In my program KeyPressed (start holding a key) is not working but KeyReleased (release the key) is. I've done similar things before and I'm sure this is supposed to work. Here is the relevant part of the code.

Code: [Select]
...
while (app->IsOpened())
{
...
sf::Event ev;
...
if (ev.Type == sf::Event::KeyPressed)
{
if (ev.Key.Code == sf::Key::W)
{
exit(0); // not working
}
}
if (ev.Type == sf::Event::KeyReleased)
{
if (ev.Key.Code == sf::Key::W)
{
exit(0); // working
}
}
...
}
...



Here is the while thing.

Code: [Select]
#include <iostream>
#include <windows.h>
#include <time.h>
#include "SFML\Graphics.hpp"
#include "SFML\Window.hpp"

sf::Window* openGL_setup()
{
// Set color and depth clear value
glClearDepth(1.f);
glClearColor(0.f, 0.f, 0.f, 0.f);

// Enable Z-buffer read and write
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);

// Setup a perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.f, 1.f, 1.f, 500.f);

sf::WindowSettings window_settings;
window_settings.DepthBits = 24;
window_settings.StencilBits = 8;
window_settings.AntialiasingLevel = 0;

return new sf::Window(sf::VideoMode(1200, 800, 32), "", sf::Style::Close, window_settings);
}

void per_frame_openGL_setup()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void main()
{
sf::Window* app = openGL_setup();

// framerate
double target_seconds_per_frame = 0.02; // or "... = 1 / fps"
double frame_start_time;

while (app->IsOpened())
{
frame_start_time = clock();
sf::Event ev;
while (app->GetEvent(ev))
{
}
if (ev.Type == sf::Event::Resized)
{
glViewport(0, 0, ev.Size.Width, ev.Size.Height);
}
if (ev.Type == sf::Event::Closed)
{
app->Close();
}
if (ev.Type == sf::Event::KeyPressed)
{
if (ev.Key.Code == sf::Key::W)
{
exit(0); // not working
}
}
if (ev.Type == sf::Event::KeyReleased)
{
if (ev.Key.Code == sf::Key::W)
{
exit(0); // working
}
}
app->SetActive();
per_frame_openGL_setup();
// replace this line with drawing stuff
app->Display();
// framerate
double seconds_for_last_frame = (clock() - frame_start_time) / CLOCKS_PER_SEC;
double sleep_time_for_frame_in_seconds = target_seconds_per_frame - seconds_for_last_frame;
if (sleep_time_for_frame_in_seconds < 0)
{
sleep_time_for_frame_in_seconds = 0;
}
Sleep(sleep_time_for_frame_in_seconds * 1000);
}
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
KeyPressed not working but KeyReleased is
« Reply #1 on: April 28, 2010, 07:45:37 pm »
Code: [Select]
     while (app->GetEvent(ev))
      {
      }
You have to put the event reactions inside the while loop, otherwise you handle only the last event of each frame.

By the way, int main() is the correct declaration and you should free the memory allocated by new.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

alan2here

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
KeyPressed not working but KeyReleased is
« Reply #2 on: April 29, 2010, 11:06:10 am »
Quote from: "Nexus"
Code: [Select]
     while (app->GetEvent(ev))
      {
      }
You have to put the event reactions inside the while loop, otherwise you handle only the last event of each frame.

ty.

 

anything