具体代码已经上传资源,可供下载。https://download.csdn.net/download/qq_34935373/11430248
项目结构如下:
.pro文件如下
#-------------------------------------------------
#
# Project created by QtCreator 2019-06-04T00:46:01
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = read
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
CONFIG += static
SOURCES += \
main.cpp \
mainwindow.cpp \
joystick.cpp \
joy_thread.cpp \
HEADERS += \
mainwindow.h \
joystick.h \
joy_thread.h \
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
main.cpp如下:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
joystick.h和.cpp文件如下
#ifndef JOYSTICK_H
#define JOYSTICK_H
#include <QLibrary>
//#include <QDebug>
#include <QString>
#include "windows.h"
#include "winuser.h"
//采用多媒体库轮询读取手柄的好处是可以方便地自定义手柄组合按键
#include "mmsystem.h"
#define JSX JOYSTICKID1 //手柄ID号
//dll库函数声明
typedef int (*MyPrototype) (HWND, UINT, UINT, BOOL);
extern MyPrototype qJoySetCapture;
typedef void (*MyPrototype1) (UINT);
extern MyPrototype1 qJoyReleaseCapture ;
typedef void (*MyPrototype2) (UINT, LPJOYINFOEX);
extern MyPrototype2 qJoyGetPosEx;
typedef void (*MyPrototype0) (UINT, LPJOYINFO);
extern MyPrototype0 qJoyGetPos;
typedef void (*MyPrototype3) (UINT, LPUINT);
extern MyPrototype3 qJoyGetThreshold ;
typedef void (*MyPrototype4) (UINT, LPJOYCAPS, UINT);
extern MyPrototype4 qJoyGetDevCaps;
typedef UINT (*MyPrototype5) (void);
extern MyPrototype5 qJoyGetNumDevs;
//joystick外部接口
void openJoy();
struct joyinfoex_tag joyRead_row();
#endif // JOYSTICK_H
#include "joystick.h"
#include <QMessageBox>
#include <QDebug>
static joyinfoex_tag now_act;
//加载多媒体库
QLibrary mylib("Winmm.dll");
//解析游戏杆捕获函数joySetCapture
MyPrototype qJoySetCapture = (MyPrototype)mylib.resolve("joySetCapture");
//游戏杆数获得函数
MyPrototype1 qJoyReleaseCapture = (MyPrototype1)mylib.resolve("joyReleaseCapture");
//返回游戏杆位置及按钮活动函数
MyPrototype2 qJoyGetPosEx = (MyPrototype2)mylib.resolve("joyGetPosEx");
//返回游戏杆位置及按钮活动函数
MyPrototype0 qJoyGetPos = (MyPrototype0)mylib.resolve("joyGetPos");
//读取操纵杆移动阈值
MyPrototype3 qJoyGetThreshold = (MyPrototype3)mylib.resolve("joyGetThreshold");
//获取操纵杆属性信息,以结构体JoyCaps接收
MyPrototype4 qJoyGetDevCaps = (MyPrototype4)mylib.resolve("joyGetDevCapsW");
//有多少个游戏杆可以用
MyPrototype5 qJoyGetNumDevs = (MyPrototype5)mylib.resolve("joyGetNumDevs");
//打开手柄,开启对手柄的检测功能
void openJoy()
{
JOYCAPS joycaps;
qJoyGetDevCaps(JSX, &joycaps, sizeof(joycaps));
}
//轮询检测手柄按键函数
struct joyinfoex_tag joyRead_row()
{
now_act.dwSize = sizeof(joyinfoex_tag);
now_act.dwFlags = (int)JOY_RETURNBUTTONS;
qJoyGetPosEx(JSX, &now_act);
return now_act;
}
joy_thread.h和cpp文件如下
#ifndef JOY_THREAD_H
#define JOY_THREAD_H
#include <QThread>
//#include <QDebug>
#include "joystick.h"
#include <string>
#include <iostream>
#include <fstream>
//#include <stdio.h>
//#include <stdlib.h>
#define JOY_READ_PEROID 100 //读取手柄周期,单位ms
struct Joykey_Info
{
bool A_botton=0;
bool B_botton=0;
bool X_botton=0;
bool Y_botton=0;
bool LB_botton=0;
bool RB_botton=0;
bool BACK_botton=0;
bool START_botton=0;
bool UP_botton=0;
bool DOWN_botton=0;
bool LEFT_botton=0;
bool RIGHT_botton=0;
int X_Axis=0;
int Y_Axis=0;
int U_Axis=0;
int R_Axis=0;
int Z_Axis=0;
};
class Joy_Thread : public QThread
{
Q_OBJECT
public:
struct Joykey_Info joykey_info; //test
joyinfoex_tag state_row;
std::ofstream file;
// FILE *fp;
explicit Joy_Thread(QObject *parent = 0);
void copy_value_to_joykey_info();
signals:
void JoySignal_row(joyinfoex_tag); //test
protected:
void run();
void send_state_row(joyinfoex_tag state_row);
};
#endif // JOY_THREAD_H
#include "joy_thread.h"
#include <QDebug>
Joy_Thread::Joy_Thread(QObject *parent) :
QThread(parent)
{
}
//发送joy的状态结构体信号
void Joy_Thread::send_state_row(joyinfoex_tag state_row)
{
emit JoySignal_row(state_row);
}
//Joy_Thread的线程主体
void Joy_Thread::run()
{
//joyinfoex_tag state_row;
openJoy();
std::ofstream file;
while(1) {
msleep(JOY_READ_PEROID);
state_row = joyRead_row();
copy_value_to_joykey_info();
// fp=fopen("joy_keyinfo.txt","wb");
// fwrite(&joykey_info,sizeof(joykey_info),1,fp);
// fclose(fp);
file.open("joy_keyinfo.txt",std::ios_base::out);
file<<joykey_info.A_botton<<" "
<<joykey_info.B_botton<<" "
<<joykey_info.X_botton<<" "
<<joykey_info.Y_botton<<" "
<<joykey_info.LB_botton<<" "
<<joykey_info.RB_botton<<" "
<<joykey_info.BACK_botton<<" "
<<joykey_info.START_botton<<" "
<<joykey_info.UP_botton<<" "
<<joykey_info.DOWN_botton<<" "
<<joykey_info.LEFT_botton<<" "
<<joykey_info.RIGHT_botton<<" "
<<joykey_info.X_Axis<<" "
<<joykey_info.Y_Axis<<" "
<<joykey_info.U_Axis<<" "
<<joykey_info.R_Axis<<" "
<<joykey_info.Z_Axis;
file.close();
send_state_row(state_row);
}
}
void Joy_Thread::copy_value_to_joykey_info()
{
joykey_info.X_Axis=(state_row.dwXpos);
joykey_info.Y_Axis=(state_row.dwYpos);
joykey_info.U_Axis=(state_row.dwUpos);
joykey_info.R_Axis=(state_row.dwRpos);
joykey_info.Z_Axis=(state_row.dwZpos);
if(state_row.dwButtons & 0x01 << 0) {
joykey_info.A_botton=1;
}
else{joykey_info.A_botton=0;}
if(state_row.dwButtons & 0x01 << 1) {
joykey_info.B_botton=1;
}else{ joykey_info.B_botton=0;}
if(state_row.dwButtons & 0x01 << 2) {
joykey_info.X_botton=1;
}else{joykey_info.X_botton=0;}
if(state_row.dwButtons & 0x01 << 3) {
joykey_info.Y_botton=1;
}else{
joykey_info.Y_botton=0;
}
if(state_row.dwButtons & 0x01 << 4) {
joykey_info.LB_botton=1;
}else{
joykey_info.LB_botton=0;
}
if(state_row.dwButtons & 0x01 << 6) {
joykey_info.BACK_botton=1;
}else{
joykey_info.BACK_botton=0;
}
if(state_row.dwButtons & 0x01 << 7) {
joykey_info.START_botton=1;
}else{ joykey_info.START_botton=0;}
if(state_row.dwButtons & 0x01 << 5) {
joykey_info.RB_botton=1;
}else{joykey_info.RB_botton=0;}
if(state_row.dwPOV == 0) {
joykey_info.UP_botton=1;
} else if(state_row.dwPOV == 9000) {
joykey_info.RIGHT_botton=1;
} else if(state_row.dwPOV == 18000) {
joykey_info.DOWN_botton=1;
} else if(state_row.dwPOV == 27000) {
joykey_info.LEFT_botton=1;
} else if(state_row.dwPOV == 4500) {
joykey_info.UP_botton=1;
joykey_info.RIGHT_botton=1;
} else if(state_row.dwPOV == 31500) {
joykey_info.UP_botton=1;
joykey_info.LEFT_botton=1;
} else if(state_row.dwPOV == 13500) {
joykey_info.RIGHT_botton=1;
joykey_info.DOWN_botton=1;
} else if(state_row.dwPOV == 22500) {
joykey_info.LEFT_botton=1;
joykey_info.DOWN_botton=1;
}else{
joykey_info.LEFT_botton=0;
joykey_info.DOWN_botton=0;
joykey_info.UP_botton=0;
joykey_info.RIGHT_botton=0;
}
}
mainwinodws.h和.cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "joy_thread.h"
#include "QThread"
//#include "QDebug"
//struct Joykey_Info
//{
// bool A_botton=0;
// bool B_botton=0;
// bool X_botton=0;
// bool Y_botton=0;
// bool LB_botton=0;
// bool RB_botton=0;
// bool BACK_botton=0;
// bool START_botton=0;
// bool UP_botton=0;
// bool DOWN_botton=0;
// bool LEFT_botton=0;
// bool RIGHT_botton=0;
// int X_Axis=0;
// int Y_Axis=0;
// int U_Axis=0;
// int R_Axis=0;
// int Z_Axis=0;
//};
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
// Joykey_Info joykey_info;
//
private slots:
void display_slot_row(joyinfoex_tag state_row);
private:
void clearDisplay();
Ui::MainWindow *ui;
Joy_Thread *joy_thread;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
const QString BTN_color("background-color: rgb(255, 32, 85);");
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
joy_thread = new Joy_Thread();
// Joykey_Info joykey_info;
qRegisterMetaType<joyinfoex_tag>("joyinfoex_tag");//注册一种信号的参数类型
connect(joy_thread, SIGNAL(JoySignal_row(joyinfoex_tag)), this, SLOT(display_slot_row(joyinfoex_tag)));
joy_thread->start();
}
MainWindow::~MainWindow()
{
delete ui;
// fileOutput.close();
}
void MainWindow::clearDisplay()
{
ui->PB_A->setStyleSheet("");
ui->PB_B->setStyleSheet("");
ui->PB_X->setStyleSheet("");
ui->PB_Y->setStyleSheet("");
ui->PB_LB->setStyleSheet("");
ui->PB_LT->setStyleSheet("");
ui->PB_RB->setStyleSheet("");
ui->PB_RT->setStyleSheet("");
ui->POV_F->setStyleSheet("");
ui->POV_B->setStyleSheet("");
ui->POV_L->setStyleSheet("");
ui->POV_R->setStyleSheet("");
ui->PB_BACK->setStyleSheet("");
ui->PB_START->setStyleSheet("");
ui->SK_LU->setStyleSheet("");
ui->SK_LD->setStyleSheet("");
ui->SK_LR->setStyleSheet("");
ui->SK_LL->setStyleSheet("");
ui->PB_LP->setStyleSheet("");
ui->SK_RU->setStyleSheet("");
ui->SK_RD->setStyleSheet("");
ui->SK_RL->setStyleSheet("");
ui->SK_RR->setStyleSheet("");
ui->PB_RP->setStyleSheet("");
}
void MainWindow::display_slot_row(joyinfoex_tag state_row)
{
QString s;
s.append("dwSize=").append(QString::number(state_row.dwSize)).append("\n");
s.append("dwFlags=").append(QString::number(state_row.dwFlags)).append("\n");
s.append("dwXpos=").append(QString::number(state_row.dwXpos)).append("\n");
s.append("dwYpos=").append(QString::number(state_row.dwYpos)).append("\n");
s.append("dwZpos=").append(QString::number(state_row.dwZpos)).append("\n");
s.append("dwRpos=").append(QString::number(state_row.dwRpos)).append("\n");
s.append("dwUpos=").append(QString::number(state_row.dwUpos)).append("\n");
s.append("dwVpos=").append(QString::number(state_row.dwVpos)).append("\n");
s.append("dwButtons=").append(QString::number(state_row.dwButtons)).append("\n");
s.append("dwButtonNumber=").append(QString::number(state_row.dwButtonNumber)).append("\n");
s.append("dwPOV=").append(QString::number(state_row.dwPOV)).append("\n");
s.append("dwReserved1=").append(QString::number(state_row.dwReserved1)).append("\n");
s.append("dwReserved2=").append(QString::number(state_row.dwReserved2)).append("\n");
ui->textEdit->setText(s);
// joykey_info.X_Axis=(state_row.dwXpos);
// joykey_info.Y_Axis=(state_row.dwYpos);
// joykey_info.U_Axis=(state_row.dwUpos);
// joykey_info.R_Axis=(state_row.dwRpos);
// joykey_info.Z_Axis=(state_row.dwZpos);
clearDisplay();
if(state_row.dwXpos > 65000) {
ui->SK_LR->setStyleSheet(BTN_color);
} else if(state_row.dwXpos < 500) {
ui->SK_LL->setStyleSheet(BTN_color);
}
if(state_row.dwYpos > 65000) {
ui->SK_LD->setStyleSheet(BTN_color);
} else if(state_row.dwYpos < 500) {
ui->SK_LU->setStyleSheet(BTN_color);
}
if(state_row.dwRpos > 65000) {
ui->SK_RD->setStyleSheet(BTN_color);
} else if(state_row.dwRpos < 500) {
ui->SK_RU->setStyleSheet(BTN_color);
}
if(state_row.dwUpos > 65000) {
ui->SK_RR->setStyleSheet(BTN_color);
} else if(state_row.dwUpos < 500) {
ui->SK_RL->setStyleSheet(BTN_color);
}
if(state_row.dwZpos > 65000) {
ui->PB_LT->setStyleSheet(BTN_color);
} else if(state_row.dwZpos < 500) {
ui->PB_RT->setStyleSheet(BTN_color);
}
if(state_row.dwButtons & 0x01 << 0) {
ui->PB_A->setStyleSheet(BTN_color);
//joykey_info.A_botton=1;
//qDebug()<<joykey_info.A_botton;
}
if(state_row.dwButtons & 0x01 << 1) {
ui->PB_B->setStyleSheet(BTN_color);
// qDebug()<<"B";
// joykey_info.B_botton=1;
}
if(state_row.dwButtons & 0x01 << 2) {
ui->PB_X->setStyleSheet(BTN_color);
// qDebug()<<"X";
//joykey_info.X_botton=1;
}
if(state_row.dwButtons & 0x01 << 3) {
ui->PB_Y->setStyleSheet(BTN_color);
//qDebug()<<"Y";
//joykey_info.Y_botton=1;
}
if(state_row.dwButtons & 0x01 << 4) {
ui->PB_LB->setStyleSheet(BTN_color);
//qDebug()<<"LB";
//joykey_info.LB_botton=1;
}
if(state_row.dwButtons & 0x01 << 6) {
ui->PB_BACK->setStyleSheet(BTN_color);
//qDebug()<<"back"
// joykey_info.BACK_botton=1;
}
if(state_row.dwButtons & 0x01 << 7) {
ui->PB_START->setStyleSheet(BTN_color);
//qDebug()<<"start";
// joykey_info.START_botton=1;
}
if(state_row.dwButtons & 0x01 << 5) {
ui->PB_RB->setStyleSheet(BTN_color);
// joykey_info.RB_botton=1;
}
if(state_row.dwButtons & 0x01 << 8) {
ui->PB_LP->setStyleSheet(BTN_color);
}
if(state_row.dwButtons & 0x01 << 9) {
ui->PB_RP->setStyleSheet(BTN_color);
}
if(state_row.dwPOV == 0) {
ui->POV_F->setStyleSheet(BTN_color);
//joykey_info.UP_botton=1;
} else if(state_row.dwPOV == 9000) {
ui->POV_R->setStyleSheet(BTN_color);
// joykey_info.RIGHT_botton=1;
} else if(state_row.dwPOV == 18000) {
ui->POV_B->setStyleSheet(BTN_color);
// joykey_info.DOWN_botton=1;
} else if(state_row.dwPOV == 27000) {
ui->POV_L->setStyleSheet(BTN_color);
// joykey_info.LEFT_botton=1;
} else if(state_row.dwPOV == 4500) {
ui->POV_F->setStyleSheet(BTN_color);
ui->POV_R->setStyleSheet(BTN_color);
//joykey_info.UP_botton=1;
// joykey_info.RIGHT_botton=1;
} else if(state_row.dwPOV == 31500) {
ui->POV_F->setStyleSheet(BTN_color);
ui->POV_L->setStyleSheet(BTN_color);
// joykey_info.UP_botton=1;
// joykey_info.LEFT_botton=1;
} else if(state_row.dwPOV == 13500) {
ui->POV_R->setStyleSheet(BTN_color);
ui->POV_B->setStyleSheet(BTN_color);
// joykey_info.RIGHT_botton=1;
//joykey_info.DOWN_botton=1;
} else if(state_row.dwPOV == 22500) {
ui->POV_L->setStyleSheet(BTN_color);
ui->POV_B->setStyleSheet(BTN_color);
// joykey_info.LEFT_botton=1;
// joykey_info.DOWN_botton=1;
}
}
ui界面文件如下