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

Author Topic: Overload of sf::Keyboard::isKeyPressed allowing input of vector of keys  (Read 5187 times)

0 Members and 1 Guest are viewing this topic.

dd

  • Newbie
  • *
  • Posts: 10
    • View Profile
Usually, in my games, I have a list of these ifstatements:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::W))
    // Stuff
}
(Or something similar)

Now yes, while this does work, it doesn't exactly look very neat, and sometimes, when I map over 3 keys to a single input, the line wraps and looks very messy.
A good fix would be to create an overload of the sf::Keyboard::isKeyPressed() function allowing for the input of a vector (or some other form of array) of keys. For example:
 std::vector<sf::Keyboard> Keys = {sf::Keyboard::A, sf::Keyboard::D, sf::Keyboard::W, sf::Keyboard::S};
if (sf::Keyboard::isKeyPressed(Keys)){
    //stuff
}

And this would have the same effect of this:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::D, sf::Keyboard::isKeyPressed(sf::Keyboard::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::S))){
    // Stuff
}

Yes, this feature doesn't greatly impact the actual framework much, but overall it just makes code much cleaner and easier to read.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11045
    • View Profile
    • development blog
    • Email
Re: Overload of sf::Keyboard::isKeyPressed allowing input of vector of keys
« Reply #1 on: October 30, 2024, 11:18:12 pm »
It's highly recommended to use events to track inputs in most cases. Polling the "hardware" isn't very efficient and some systems require additional permissions (e.g. macOS requires monitoring permission).

There are a lot of ways to handle key mappings - Thor for example used an useful action map - as such SFML leaves this to the user or extension libraries.
The philosophy for SFML is to provide the cross-platform API on which people can build their systems.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Garwin

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: Overload of sf::Keyboard::isKeyPressed allowing input of vector of keys
« Reply #2 on: November 01, 2024, 12:23:44 pm »
You can use variadic templates but is quite unsafe.
I did not check the code, hope it is correct.

auto isAnyKeyPressed(auto... keys) -> bool
{
    return (sf::Keyboard::isKeyPressed(keys) || ...);
}
 

Now you can use just:
if (isAnyKeyPressed(sf::Keyboard::A, sf::Keyboard::D, sf::Keyboard::W, sf::Keyboard::S))
    // make stuff there

 

Problem with the code is:
1. It does not check type of arguments but at least it will generate errors if any parameter is not convertible to enum sf::Keyboard::Key
2. It has even more issues as sf::Keyboard::Key is unscoped enum which can make error messages even tougher to get to the point of error. And it can accept even incorrect parameter.

You can limit template with Concept but issue of unscoped Enum remains.

 

anything