-
Hello, this is my first post here, so I don't know if it's the right place to ask, but I have an issue with SFML.
The "sf::Keyboard::" works, but only with the F1 to F12 and with Num1 to Num0, it doesn't works with the letters or anything else, I don't know why.
Thanks if you can help, you can answer in french.
edit:
I work with VSCode on Debian 12 and I build with g++
-
Can you share some minimal and compiable code that reproduces the issue?
-
Yes, I have a function thats takes input once, and not every frames here it is:
int isPressed(sf::Event event,sf::Keyboard::Key key){
if(event.type == (sf::Event::KeyPressed)){
if (event.key.code == key&&function_done==0){
function_done=1;
return 0;
};};
if(event.type == (sf::Event::KeyReleased)){
if (event.key.code == key&&function_done==1)function_done=0;
};
return 1;
};
So it works with all keys I mentionned above but not with letters.
-
That's some interesting code formatting :D
(Btw. you don't need a semicolon after an if-body)
When you say for "letters" are you passing in as key for example sf::Keyboard::A?
-
Yes, that's exactly that I tried O,P,C,A,V none worked, so I assume none letter is working.
And I just love semicolons lol
-
Are you using a special keyboard layout?
So the following example application doesn't work?
#include <SFML/Graphics.hpp>
int main()
{
auto window = sf::RenderWindow{ { 1920u, 1080u }, "Color Switcher" };
window.setFramerateLimit(144);
auto backgroundColor = sf::Color::Black;
while (window.isOpen())
{
for (auto event = sf::Event{}; window.pollEvent(event);)
{
if (event.type == sf::Event::Closed)
{
window.close();
}
else if (event.type == sf::Event::KeyPressed && event.code == sf::Keyboard::R)
{
backgroundColor = sf::Color::Red;
}
else if (event.type == sf::Event::KeyRelease && event.code == sf::Keyboard::G)
{
backgroundColor = sf::Color::Green;
}
}
window.clear(backgroundColor);
window.display();
}
}
If you use SFML 2.6, can you try scancodes instead?
-
I installed SFML with apt-get so I have the 2.6 (I guess?) and I don't know why but the event.code you gave me doesn't work
-
Set a break point inside the key pressed event body, run the application through a debugger and check what the value of the key code is.
I have a feeling that you either have a different layout configured that you're expecting, so hitting the specific key doesn't actually trigger something. Or you have something installed on Debian that remaps keyboard values.
It's unlikely an SFML issue, since we've been using basically the same code for a long time now.
-
I did this, and no letters are detected.
if(event.type == (sf::Event::KeyPressed)){
std::cout << rand()%255 << std::endl;
};
Only functions keys, top keyboard numbers and some keys like "inser" "end" "page up" "page down" etc
The problem is also maybe because I have an french Azerty keyboard.
-
What version of SFML are you using?
-
I'm using SFML 2.6
-
What window manager are you using? Is this running wayland or xlib?
-
I'm using Xlib, x11 to be more precise
-
UPDATE:
I fixed the problem by modifying my function:
int isPressed(sf::Event event,sf::Keyboard::Key key){
if (sf::Keyboard::isKeyPressed(key)&&function_done==0){
function_done=1;
return 0;
};
if(event.type == (sf::Event::KeyReleased)){
if (event.key.code == key&&function_done==1){
function_done=0;
}else
if (!(sf::Keyboard::isKeyPressed(key))){
function_done=0;
};
};
return 1;
};
So it fixed two problems:
now letters works
and now it works instantly because before you needed to input the key at least once before it works because the function was calling the keyrealsed event :)
thanks for the help also :)
-
While sf::Keyboard::isKeyPressed may "work", it's not the intended usage to mix it with events.
Unfortunately, I don't know what's causing the events to not have the right value.