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

Author Topic: joystick detection  (Read 4431 times)

0 Members and 1 Guest are viewing this topic.

djarkan

  • Newbie
  • *
  • Posts: 16
    • View Profile
joystick detection
« on: October 16, 2022, 10:45:05 pm »
hi

tested this code to see if my gamepad was detected, but no
ok with steam and dowloaded joystick tester and ok with it.
also ok in windows joystick configuration

thanks

#include <iostream>

#include <SFML/Window/Joystick.hpp>

int main()
{
    unsigned int joystckID{0};
    sf::Joystick myJoystick;
    while(joystckID < 8) {
        if (myJoystick.isConnected(joystckID)) {
            unsigned int buttonCount = myJoystick.getButtonCount(joystckID);
            std::cout << "joystick ID " << joystckID << " conneted with"  << buttonCount << " buttons.\n";
        }
        else { std::cout << "joystick ID " << joystckID << " not conneted\n"; }
        ++joystckID;
    }
    while(1) {
        if (sf::Joystick::isButtonPressed(0, 1)) { std::cout << "button 1 pressed\n"; }
    }
      return 0;
}

 
« Last Edit: October 16, 2022, 11:02:36 pm by djarkan »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: joystick detection
« Reply #1 on: October 17, 2022, 12:24:19 am »
Hey
Unlike the keyboard or mouse, the state of joysticks is sometimes not directly available (depending on the OS), therefore an update() function must be called in order to update the current state of joysticks. When you have a window with event handling, this is done automatically, you don't need to call anything. But if you have no window, or if you want to check joysticks state before creating one, you must call sf::Joystick::update explicitly.
Try calling sf::Joystick::update() once before querying the states of your joysticks.

By the way, all sf::Joystick functions are static, you don't have to (and probably shouldn't) instantiate one.

djarkan

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: joystick detection
« Reply #2 on: October 17, 2022, 01:09:21 am »
thx working

i ve read that yesterday and i forgot today ...

one last thing

i need to press button 2 to have the message button   pressed

depends on the driver ?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: joystick detection
« Reply #3 on: October 17, 2022, 01:35:01 am »
Probably because you're not flushing your output.
End your cout with << std::flush or << std::endl (endl outputs a newline and flushes)

djarkan

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: joystick detection
« Reply #4 on: October 17, 2022, 07:30:39 pm »
my bad

was testing button 1 instead of button 0