手柄控制demo

#include<Windows.h>
#include<iostream>
#include<math.h>

#include <thread>
//添加joystick操作api的支持库
#include<MMSystem.h>
#pragma comment(lib, "Winmm.lib")

#pragma warning(disable:4996)


#define MouseSpeed 20.0
#define MouseWSpeed 20

using namespace std;


POINT lpPoint;
JOYINFO joyinfo;//定义joystick信息结构体
JOYINFOEX joyinfoex;
MMRESULT joyreturn;
void info() {
	//手柄信息
	joyinfoex.dwSize = sizeof(JOYINFOEX);
	joyinfoex.dwFlags = JOY_RETURNALL;
	while (1) {
		joyreturn = joyGetPosEx(JOYSTICKID1, &joyinfoex);
		//cout << joyinfoex.dwButtons << endl;
		Sleep(30);
	}
}
void mouseM() {
	while (1) {
		GetCursorPos(&lpPoint);
		if (joyreturn == JOYERR_NOERROR)
		{
			if (joyinfoex.dwXpos != 32767 || (joyinfoex.dwYpos != 32766 && joyinfoex.dwYpos != 32767))
			{
				//鼠标移动
				float mouseX = joyinfoex.dwXpos * 1.0 / 32766.0 - 1;
				float mouseY = joyinfoex.dwYpos * 1.0 / 32766.0 - 1;
				if (abs(mouseX) <= 0.95) {
					mouseX *= 0.8;
				}
				if (abs(mouseY) <= 0.95) {
					mouseY *= 0.8;
				}
				SetCursorPos(float(lpPoint.x) + MouseSpeed * mouseX, lpPoint.y * 1.0 + MouseSpeed * mouseY);
			}
		}
		Sleep(10);
	}
}

void mouseC() {
	//鼠标点击
	while (1) {
		if (joyinfoex.dwButtons == 1)
		{
			//左键
			mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, lpPoint.x, lpPoint.y, 0, 0);
			Sleep(300);
		}
		else if (joyinfoex.dwButtons == 2)
		{
			//右键
			mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, lpPoint.x, lpPoint.y, 0, 0);
			Sleep(300);
		}
		Sleep(10);
	}
}

void mouseW() {
	//滚轮
	while (1) {
		if (joyinfoex.dwPOV == JOY_POVFORWARD) {
			mouse_event(MOUSEEVENTF_WHEEL, 0, 0, MouseWSpeed, 0);
		}
		else if (joyinfoex.dwPOV == JOY_POVBACKWARD) {
			mouse_event(MOUSEEVENTF_WHEEL, 0, 0, -MouseWSpeed, 0);
		}
		Sleep(10);
	}
}

void kb() {
	//键盘
	while (1) {
		if (joyinfoex.dwPOV == JOY_POVLEFT) {
			keybd_event(37, 0, 0, 0);
			Sleep(100);
		}
		else if (joyinfoex.dwPOV == JOY_POVRIGHT) {
			keybd_event(39, 0, 0, 0);
			Sleep(100);
		}
		Sleep(10);
	}
}

void music() {
	while (1)
	{
		if (joyinfoex.dwPOV == JOY_POVLEFT)
		{
			keybd_event(VK_LCONTROL, 0, 0, 0);//按下CTRL
			keybd_event(VK_F5, 0, 0, 0);//按下F5
			keybd_event(VK_F5, 0, KEYEVENTF_KEYUP, 0);//松开F5
			keybd_event(VK_LCONTROL, 0, KEYEVENTF_KEYUP, 0);//松开CTRL
			Sleep(500);
		}
		else if (joyinfoex.dwPOV == JOY_POVRIGHT) {
			keybd_event(VK_LCONTROL, 0, 0, 0);//松开CTRL
			keybd_event(VK_F6, 0, 0, 0);//按下F5
			keybd_event(VK_F6, 0, KEYEVENTF_KEYUP, 0);//按下F5
			keybd_event(VK_LCONTROL, 0, KEYEVENTF_KEYUP, 0);//松开CTRL
			Sleep(500);

		}
		else if (joyinfoex.dwButtons == 4) {
			keybd_event(VK_LCONTROL, 0, 0, 0);//松开CTRL
			keybd_event(VK_F7, 0, 0, 0);//按下F7
			keybd_event(VK_F7, 0, KEYEVENTF_KEYUP, 0);//按下F7
			keybd_event(VK_LCONTROL, 0, KEYEVENTF_KEYUP, 0);//松开CTRL
			Sleep(500);

		}
		Sleep(100);
	}
}


void hint() {
	system("mode con cols=30 lines=7");
	system("color a");
	cout << "made by MJJ" << endl;
	cout << "L摇杆\t鼠标" << endl;
	cout << "A\t左键" << endl;
	cout << "B\t左键" << endl;
	cout << "上/下\t滚轮上/下" << endl;
	cout << "左/右\t上/下一首歌" << endl;
	cout << "X\t暂停/播放" << endl;
}
int main(int argc, char* argv[])
{
	hint();
	std::thread t_mouseM(mouseM);
	std::thread t_info(info);
	std::thread t_mouseC(mouseC);
	std::thread t_mouseW(mouseW);
	std::thread t_music(music);
	t_mouseM.join();
	t_info.join();
	t_mouseC.join();
	t_mouseW.join();
	t_music.join();

	while (1) {
		Sleep(1000);
	}
	return 0;
}

通过C++调用joystick实现

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值