首页 > 编程知识 正文

ps4手柄安卓按键映射,xbox手柄映射键盘

时间:2023-05-04 21:45:37 阅读:108185 作者:2483

main.c

#include string.h

#include stdint.h

#include stdio.h

#include unistd.h

#include 'joy.h '

#include 'vkey.h '

#include linux/input.h

#define MASK_UP 1

#define MASK_DOWN 2

#define MASK_LEFT 4

#define MASK_RIGHT 8

输入主(语音) )。

{

int key,value,mask=0;

printf(programebyqbasicJacky! rn ';

if(openJoy(/dev/input/js0 ) (0) )。

{

printf('nogamepad!' );

返回1;

}

#if 1

if(openvkey ) ) (0) ) ) ) ) ) ) if(openvkey ) ) ) ) ) ) if ) openv key ) ) )0) ) ) ) ) openv key ) ) ) ) ) )0) )0)0)

{

打印(cannotopenvkey! rn ';

closejoy (;

返回2;

}

#endif

打印(hello worldrn );

while(1)。

{

if(readJoy(key,value )==0) ) ) ) ) ) ) )。

{

printf(key=%dvalue=%drn ',key,value );

if(key==19 ) break;

if(key==25 ) )

{

if(value==-32767 () ) ) ) ) ) ) ) )。

{

报告vkey (ev _ key,KEY_UP,1 );

mask|=MASK_UP;

}

ELSEif(value==32767 ) ) ) ) ) ) ) )。

{

报告vkey (ev _ key,KEY_DOWN,1 );

mask|=MASK_DOWN;

}

elseif(value==0) ) )。

{

if(maskmask_up )。

{

报告vkey (ev _ key,KEY_UP,0 );

mask=~MASK_UP;

}

if(maskmask_down ) )为

{

报告vkey (ev _ key,KEY_DOWN,0 );

mask=~MASK_DOWN;

}

}

报告vkey (ev _ syn,SYN_REPORT,0 );

}

elseif(key==24 ) ) )。

{

if(value==-32767 () ) ) ) ) ) ) ) )。

{

报告vkey (ev _ key,KEY_LEFT,1 );

mask|=MASK_LEFT;

}

ELSEif(value==32767 ) ) ) ) ) ) ) )。

{

报告vkey (ev _ key,KEY_RIGHT,1 );

马克|=马克

_RIGHT;
                }
                else if(value==0)
                {
                    if(mask&MASK_LEFT)
                    {
                        reportvkey(EV_KEY, KEY_LEFT, 0);
                        mask&=~MASK_LEFT;
                    }
                    if(mask&MASK_RIGHT)
                    {
                        reportvkey(EV_KEY, KEY_RIGHT, 0);
                        mask&=~MASK_RIGHT;
                    }
                }
                reportvkey(EV_SYN, SYN_REPORT, 0);
            }
            else if(key==11)    
            {
                reportvkey(EV_KEY, KEY_SPACE, value);
                reportvkey(EV_SYN, SYN_REPORT, 0);
            }
            else if(key==13)
            {
                reportvkey(EV_KEY, KEY_ESC, value);
                reportvkey(EV_SYN, SYN_REPORT, 0);
            }

        }else usleep(1000);
    }
    closevkey();        
    closejoy();
    return 0;
}

vkey.h  vkey.c

#ifndef __VKEY_H__
#define __VKEY_H__
int reportvkey(uint16_t type, uint16_t keycode, int32_t value);
int openvkey(void);
void closevkey(void);
#endif

#include <linux/input.h>
#include <linux/uinput.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>

static int fd;
int reportvkey(uint16_t type, uint16_t keycode, int32_t value)
{
    int ret;
    struct input_event ev;
    memset(&ev, 0, sizeof(struct input_event));
    ev.type = type;
    ev.code = keycode;
    ev.value = value;
    ret = write(fd, &ev, sizeof(struct input_event));
    if (ret < 0) {
        printf("report key error!n");
        return ret;
    }
    return 0;
}

int openvkey(void)
{
    struct uinput_user_dev uidev;
    int ret,i;
    //鎵撳紑uinput璁惧
    fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
    if (fd < 0) {
        printf("error:%drn",fd);
        return fd;
    }

    //娉ㄥ唽浜嬩欢
    ioctl(fd, UI_SET_EVBIT, EV_SYN);    //鍚屾浜嬩欢
    ioctl(fd, UI_SET_EVBIT, EV_KEY);    //鎸夐敭浜嬩欢

    //娉ㄥ唽鎸夐敭
    for(i = 0; i < 128; i++){
        ioctl(fd, UI_SET_KEYBIT, i);
    }

    //鍒涘缓铏氭嫙璁惧
    memset(&uidev, 0, sizeof(struct uinput_user_dev));
    snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-sample");
    uidev.id.bustype = BUS_USB;
    uidev.id.vendor = 0x1234;    //搴旇鏄殢渚垮啓鐨?    
    uidev.id.product = 0xfedc;    //搴旇鏄殢渚垮啓鐨?    
    uidev.id.version = 1;        //搴旇鏄殢渚垮啓鐨?    
    ret = write(fd, &uidev, sizeof(struct uinput_user_dev));
    ret = ioctl(fd, UI_DEV_CREATE);
    if (ret < 0) {
        printf("create dev error:%drn",ret);
        close(fd);
        return ret;
    }
    return 0;
}

void closevkey(void)
{
    //鍒犻櫎涓嬭櫄鎷熻澶?    
    ioctl(fd, UI_DEV_DESTROY);
    close(fd);
}

joy.h joy.c

#define __JOY_H__
int openjoy(const char *dev);
int readjoy(int *key,int *value);
void closejoy(void);
#endif
#include <stdio.h>  
#include <unistd.h>  
#include <string.h>  
#include <sys/types.h>  
#include <sys/stat.h>  
#include <fcntl.h>  
#include <errno.h>  
#include <linux/input.h>  
#include <linux/joystick.h>

static int fd;
int openjoy(const char *dev)
{
    printf("open:%srn",dev);
    fd = open(dev, O_RDONLY);
    if (fd < 0)    return -1;
    return fd;
}

int readjoy(int *key,int *value)
{
    int len;
    struct js_event js;
    len = read(fd, &js, sizeof(struct js_event));
    if (len < 0)    return -1;
    *key=js.type*10+js.number;
    *value=js.value;
    return 0;
}

void closejoy(void)
{
    close(fd);
}


makefile

cc = gcc
prom = joy2key
deps = $(shell find ./ -name "*.h")
src = $(shell find ./ -name "*.c")
obj = $(src:%.c=%.o)

$(prom): $(obj)
    @echo $(src)
    $(cc) -o $(prom) $(obj)
%.o: %.c $(deps)
    $(cc) -c $< -o $@
clean:
    rm -rf $(obj) $(prom)

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。