c++手柄驱动

本文介绍了一种使用C++读取并解析Linux下游戏手柄输入的方法,通过监听/dev/input/js0设备,对js_event结构体进行解读,实现对手柄按键和摇杆动作的识别与映射。
#include <linux/joystick.h>  
#include<fstream>
#include<iostream>
#include <thread>

enum class xbox_map{
    UP,
    DOWN,
    LEFT,
    RIGHT,
    X,
    Y,
    A,
    B,
    RESET,
    START,
    NONE
};

void judge_key(js_event& js,xbox_map& data_,int last_value);
using namespace std;

int main()
{

    xbox_map data_;
    int last_value;
    last_value=10;
    const char* file="/dev/input/js0";
    ifstream fin;
    fin.open(file,ios_base::binary | ios_base::in);
    struct js_event js;
    fstream data;
    data.open("data.txt",ios::out|ios::binary);
    while (1)
    {
        fin.read((char *) &js,sizeof(js));
        cout<<"time:"<<(int)js.time<<endl<<"number:"<<(int)js.number<<endl<<"type:"<<(int)js.type<<endl<<"value:"<<(int)js.value<<endl;
        judge_key(js,data_,last_value);
        last_value=js.value;
        cout<<"l
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值