You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
#ifndef DIRECTINPUTJOYSTICK_H
|
|
|
|
|
#define DIRECTINPUTJOYSTICK_H
|
|
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
#include <dinput.h>
|
|
|
|
|
#include <QThread>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DirectInputJoystick : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
|
|
|
|
explicit DirectInputJoystick(QObject *parent = nullptr);
|
|
|
|
|
~DirectInputJoystick();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool initialize();
|
|
|
|
|
void startListening();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
signals:
|
|
|
|
|
// 轴变化信号(axis: 0-X, 1-Y, 2-Z; value: -1.0 ~ 1.0)
|
|
|
|
|
void axisChanged(int axis, double value);
|
|
|
|
|
// 按钮状态变化(button: 按钮编号,从0开始;pressed: 是否按下)
|
|
|
|
|
void buttonChanged(int button, bool pressed);
|
|
|
|
|
// 方向按钮状态变化(direction: 按钮编号,上下左右;pressed: 是否按下)
|
|
|
|
|
void directionChanged(int direction);
|
|
|
|
|
// 错误信号
|
|
|
|
|
void errorOccurred(const QString &message);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool setupDevice();
|
|
|
|
|
void cleanup();
|
|
|
|
|
static BOOL CALLBACK enumAxesCallback(const DIDEVICEOBJECTINSTANCE* doi, VOID* context);
|
|
|
|
|
static BOOL CALLBACK enumJoysticksCallback(const DIDEVICEINSTANCE* instance, VOID* context);
|
|
|
|
|
|
|
|
|
|
DIJOYSTATE lastState;
|
|
|
|
|
LPDIRECTINPUT8 m_pDirectInput = nullptr;
|
|
|
|
|
LPDIRECTINPUTDEVICE8 m_pJoystick = nullptr;
|
|
|
|
|
HANDLE m_hEvent = nullptr;
|
|
|
|
|
QThread *m_workerThread;
|
|
|
|
|
bool m_isRunning = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif // DIRECTINPUTJOYSTICK_H
|