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

Author Topic: Key press.  (Read 9114 times)

0 Members and 2 Guests are viewing this topic.

Viruses

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Key press.
« on: February 11, 2012, 09:55:21 pm »
How do you check for a SINGLE key press? So you want to check if spacebar has been PRESSED and then RELEASED. So checking for one tap.

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Key press.
« Reply #1 on: February 11, 2012, 10:08:30 pm »
Viruses, you can pull events from the window and check for release events, as the key will be released only once.

Viruses

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Key press.
« Reply #2 on: February 12, 2012, 08:37:00 pm »
Could you please be more specific as to HOW i could implement this?

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Key press.
« Reply #3 on: February 12, 2012, 08:50:10 pm »
Read this tutorial:
http://www.sfml-dev.org/tutorials/1.6/window-events.php

If you are using SFML 2.0, use PollEvent instead of GetEvent.

Viruses

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Key press.
« Reply #4 on: February 12, 2012, 10:08:59 pm »
I don't get it..

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
Key press.
« Reply #5 on: February 12, 2012, 11:21:56 pm »
Quote
So checking for one tap.
That is not possible, you can only check press or release separately.

But Tex Killer is completely right: if the key was released then it has been tabbed (or pressed for a while).

If you want to be sure that the key went up and down within a second (so when it was tapped and not held down for a while) then you have to write it yourself.
- Set a boolean to true when a button is pressed.
- Reset a timer.
- When the button is released you check how many time was passed. Less than one second -> key was tapped, more than one second -> ignore it.
- Set the boolean to false.

But yet again: just check for key release and save yourself a lot of trouble.
TGUI: C++ SFML GUI

dotty

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Key press.
« Reply #6 on: February 13, 2012, 12:42:14 pm »
Code: [Select]

w_pressed = false

if w is held
if w_pressed is false
do something while W is pressed (not held)
w_pressed = true
else
do something while W is held
else
w_pressed = false

 

anything