SDL2 / Qt C ++无法读取操纵杆输入但可以识别

时间:2018-09-27 00:40:11

标签: c++ qt sdl sdl-2 ps4

那是我的main.cpp

#include "mainwindow.h"
#include <QApplication>
#include "SDL.h"

#include <QDebug>

#undef main
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    SDL_Window *window = nullptr;
    SDL_Surface *windowSurface = nullptr;

    SDL_Init(SDL_INIT_VIDEO);
    SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,"1");
    SDL_Init( SDL_INIT_JOYSTICK);

    SDL_Joystick *joystick = SDL_JoystickOpen(0);

    qDebug()<<SDL_JoystickName(joystick);
    qDebug()<<(SDL_JoystickNumAxes(joystick));
    qDebug()<<(SDL_JoystickNumButtons(joystick));
    window = SDL_CreateWindow("SDL CodingMadeEasy Series", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
    windowSurface = SDL_GetWindowSurface(window);

    SDL_Event ev;
    int x;
    bool isRunning = true;

    while(isRunning)
    {
        while(SDL_PollEvent(&ev) != 0)
        {        
            if(ev.type == SDL_JOYBUTTONDOWN)
            {            
                qDebug()<<"Button Clicked";            
            }
        }    
        // Drawing the current image to the window    
        SDL_UpdateWindowSurface(window);
    }
    return a.exec();
}

它可以识别PS4控制器,因为它可以打印出qDebug行。 我尝试更改ev.type ==(有些东西还不是JOYSTICK相关的),而且效果很好,因此JOYBUTTONDOWN显然存在问题。

有什么帮助吗?

1 个答案:

答案 0 :(得分:2)

已通过启用操纵杆事件进行了修复

SDL_JoystickEventState(SDL_ENABLE);