手柄控制demo

手柄控制demo,第1张

#include
#include
#include

#include 
//添加joystick *** 作api的支持库
#include
#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实现

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/3002883.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-27
下一篇 2022-09-27

发表评论

登录后才能评论

评论列表(0条)

保存