具体代码已经上传资源,可供下载。https://download.csdn.net/download/qq_34935373/11430248

项目结构如下:

.pro文件如下

#-------------------------------------------------
#
# Project created by QtCreator 2019-06-04T00:46:01
#
#-------------------------------------------------QT       += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = 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.0CONFIG += c++11
CONFIG += staticSOURCES += \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 //读取手柄周期,单位msstruct 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; //testjoyinfoex_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); //testprotected: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_OBJECTpublic: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界面文件如下

基于QT读取微软xbox ones 手柄消息并写入文件相关推荐

  1. 练习:【三国游戏文件信息读取分类、键盘录入学生信息写入文件、 GBK与UTF-8文件内容互相转换】

    练习:[三国游戏文件信息读取分类.键盘录入学生信息写入文件. GBK与UTF-8文件内容互相转换] 三国游戏文件信息读取分类 txt文件信息 1 阿会喃 云南 男 190 225 74 2 伊籍 江陵 ...

  2. 配置微软Xbox One手柄到ROS实现通讯控制

    环境:Ubuntu16.04 + ROS kinetic 安装joy package joy package为通用的Linux操纵杆提供了ROS驱动,它包括了一个joy_node节点,可以让Linux ...

  3. 【MATLAB】图像批处理:批量读取图像—>批量处理—>批量写入文件夹

    创作时间:2022-03-14 目录: 1.图像批处理的应用 2.代码 3.演示结果 4.总结 正文: 1.图像批处理的应用 在做图像处理时候,首先是研究算法,在单张的图像上做分析,把算法搞明白. 接 ...

  4. ROS与Arduino:用Twist消息+用键盘+用Xbox one手柄 控制小车

    ros_arduino_brige功能包使用和安装这里就不多说了,详情点击 先看一下ros_arduino_brige功能包的文件系统 ├── README.md ├── ros_arduino_br ...

  5. xboxones手柄驱动_微软Xbox One S全数字版再次发售,1043元

    IT之家12月18日消息 今年4月份微软发布了无光驱版Xbox One S,而在今年黑五期间,迎来大优惠.现在微软Xbox One S全数字版再次发售,价格比平时便宜整整100美元. 美国亚马逊新的交 ...

  6. 如何用Xbox手柄玩家玩java我的世界,在Gear VR上玩《我的世界》,怎么能少了微软Xbox手柄...

    老手柄新平台,下月起Xbox One手柄可支持Gear VR. 这款和微软Xbox One S一起到来的Xbox无线手柄内置蓝牙,能够与安卓手机连接.所以微软才有机会和三星GearVR"联姻 ...

  7. 微软推出新版Xbox One手柄 配对更迅速

    如果你也遇到过Xbox One手柄无故失联的问题,那么微软最新推出的手柄固件更新对你来说应该是一个好消息,而这应该也是自Xbox One发布以来微软推出的第三次手柄固件更新. 据悉,本次更新将着重解决 ...

  8. sqlserver 微信 读取_基于SQLServer数据库实现微信推送消息

    本文主要向大家介绍了基于SQLServer数据库实现微信推送消息,通过具体的内容向大家展现,希望对大家学习SQLServer数据库有所帮助. 最近新上了一个项目,需要监控设备数据,增加一个报警功能. ...

  9. 微软xbox服务器切换,微软 Xbox Series X/S切换手柄功能操作步骤教程

    [惠美优普]数码资讯 对于那些使用 Xbox Series X / S 手柄进行游戏的人来说,可以轻松地在 Xbox.PC 和移动设备之间切换手柄是个不错的功能. 其实,该功能一直就有. 早在几个月前 ...

最新文章

  1. MySQL—06—数据库三大范式
  2. 关于Class之深入Class
  3. Computer Game
  4. imx6 mac地址设置
  5. Windows Phone开发手记-WinRT下启动器替代方案
  6. 最近纠结致死的一个java报错java.net.SocketException: Connection reset 终于得到解决
  7. 在OSPF网络中,如何判断LSA的新旧?
  8. 2018只剩32天,动着心思想跳槽的你,还要YY到什么时候
  9. 在使用vue-video-player时,切换页面浏览器报错:TypeError: this.el_.vjs_getProperty is not a function**
  10. 如何彻底卸载3dmax2020_完全卸载3DMAX的方法
  11. 论文笔记——Thompson Sampling for Contextual Bandits with Linear Payoffs(线性收益)
  12. softmax和sigmod的pytorch这些
  13. mysql 1045 远程_启用远程mysql连接:错误1045(28000):拒绝用户访问
  14. 树莓派实战:微信机器人(itchat实现)
  15. EXCEL 连接 ORACLE 查询数据到表格 中文乱码 中文变成?
  16. node爬取双马尾小姐姐图片
  17. 逆向教程-2014.08版电信爱游戏支付分析
  18. JVM类加载过程,JDK和JER区别
  19. 计算机综合实践免修 华东师范大学,电子技术综合实践
  20. 教师计算机培训心得博客,信息技术提高培训心得体会

热门文章

  1. 对抗样本攻防战,清华大学TSAIL团队再获CAAD攻防赛第一
  2. 基于delphi的excel数据导入导出
  3. Android开发资源分享合集
  4. 我与前端 | 视野的重要性
  5. PowerBuilder学习笔记(窗口设计)
  6. html超链接样式顺序,CSS页面布局常用知识汇总(超链接样式)
  7. 侧边栏php,关于一个社区系统「侧边栏」的实现
  8. 事件驱动组件libevent实战
  9. Vulkan 交换链详解
  10. Tiktok免越狱群控 分享Tiktok引流变现矩阵玩法