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

Author Topic: Keyboard inputs with only console  (Read 2352 times)

0 Members and 1 Guest are viewing this topic.

JOJO

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Keyboard inputs with only console
« on: April 03, 2017, 08:01:55 pm »
Hello everyone ,
So I am currently working on a project, it requires me to use only console and get the keyboard inputs in real time.
That implies that I have to handle keyboard inputs  but I don't have any window to do it.
The code is below now it compiles and all however I have a big problem whenever I press a key it counts as multiple keypresses.
(20 at least) obviously that's a big problem. I've looked around and saw the keyRepeatEnabled function however this requires a window that I don't want to use.
I hope to have been clear on my problem and you'll be able to bring a solution. How to get only one key press instead of multiple ones.
Best Regards,

void Player::player_mvt(Board &board) {
    while (!(sf::Keyboard::isKeyPressed(sf::Keyboard::Return) && board.getBoard(board.getBase().first,
                                                                                board.getBase().second).isTarget())) {
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) {

                handle_mvt(board, std::make_pair(0, -1));
            break;
        } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z)) {
            handle_mvt(board, std::make_pair(-1, 0));
            break;
        } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {

            handle_mvt(board, std::make_pair(0, 1));
            break;
        } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
            handle_mvt(board, std::make_pair(1, 0));
            break;
        } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return)) {
            std::cout << "Cannot move in this position" << std::endl;
        }
    }
}


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Keyboard inputs with only console
« Reply #1 on: April 03, 2017, 08:39:22 pm »
isKeyPressed is just a state check and not "event" like, so you'll have to adjust your code accordingly.

Save the state and only react up on state change. So if before it was not pressed and is now pressed that's your key pressed "event". If it remains pressed, no event. If it changes from pressed to not pressed, key released.
Additionally, if you want to simulate a key repeat event, introduce a clock that will act as "cool down" and after the cool down it will trigger another key pressed event.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

JOJO

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Keyboard inputs with only console
« Reply #2 on: April 03, 2017, 08:56:22 pm »
Ok so I'll add a bolean to "simulate" the key released event do I get it right?
Thank you very much for your quick answer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Keyboard inputs with only console
« Reply #3 on: April 03, 2017, 09:15:30 pm »
Yes, but you want a boolean per key.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

JOJO

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Keyboard inputs with only console
« Reply #4 on: April 03, 2017, 10:40:25 pm »
OK ! It finally works thank you so much for you time  :)

 

anything