172 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			C++
		
	
	
	
		
		
			
		
	
	
			172 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			C++
		
	
	
	
|  | #include "..\easy2d.h"
 | |||
|  | #include "..\EasyX\easyx.h"
 | |||
|  | #include <conio.h>
 | |||
|  | 
 | |||
|  | // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | static std::vector<KeyMsg*> s_vKeyMsg; | |||
|  | 
 | |||
|  | // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | static std::vector<VK_KEY> s_vKeys = { | |||
|  | 	KeyMsg::A, KeyMsg::B, KeyMsg::C, KeyMsg::D, KeyMsg::E, KeyMsg::F, KeyMsg::G, KeyMsg::H, KeyMsg::I, KeyMsg::J, | |||
|  | 	KeyMsg::K, KeyMsg::L, KeyMsg::M, KeyMsg::N, KeyMsg::O, KeyMsg::P, KeyMsg::Q, KeyMsg::R, KeyMsg::S, KeyMsg::T, | |||
|  | 	KeyMsg::U, KeyMsg::V, KeyMsg::W, KeyMsg::X, KeyMsg::Y, KeyMsg::Z, | |||
|  | 	KeyMsg::NUM_1, KeyMsg::NUM_2, KeyMsg::NUM_3, KeyMsg::NUM_4, KeyMsg::NUM_5, | |||
|  | 	KeyMsg::NUM_6, KeyMsg::NUM_7, KeyMsg::NUM_8, KeyMsg::NUM_9, KeyMsg::NUM_0, | |||
|  | 	KeyMsg::NUMPAD_1, KeyMsg::NUMPAD_2, KeyMsg::NUMPAD_3, KeyMsg::NUMPAD_4, KeyMsg::NUMPAD_5, | |||
|  | 	KeyMsg::NUMPAD_6, KeyMsg::NUMPAD_7, KeyMsg::NUMPAD_8, KeyMsg::NUMPAD_9, KeyMsg::NUMPAD_0, | |||
|  | 	KeyMsg::Enter, KeyMsg::Space, KeyMsg::Up, KeyMsg::Down, KeyMsg::Left, KeyMsg::Right, KeyMsg::Esc, | |||
|  | 	KeyMsg::Decimal, KeyMsg::Shift, KeyMsg::LShift, KeyMsg::RShift, KeyMsg::Ctrl, KeyMsg::LCtrl, KeyMsg::RCtrl, | |||
|  | 	KeyMsg::F1, KeyMsg::F2, KeyMsg::F3, KeyMsg::F4, KeyMsg::F5, KeyMsg::F6, | |||
|  | 	KeyMsg::F7, KeyMsg::F8, KeyMsg::F9, KeyMsg::F10, KeyMsg::F11, KeyMsg::F12 | |||
|  | }; | |||
|  | 
 | |||
|  | // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>
 | |||
|  | const VK_KEY KeyMsg::A = 'A'; | |||
|  | const VK_KEY KeyMsg::B = 'B'; | |||
|  | const VK_KEY KeyMsg::C = 'C'; | |||
|  | const VK_KEY KeyMsg::D = 'D'; | |||
|  | const VK_KEY KeyMsg::E = 'E'; | |||
|  | const VK_KEY KeyMsg::F = 'F'; | |||
|  | const VK_KEY KeyMsg::G = 'G'; | |||
|  | const VK_KEY KeyMsg::H = 'H'; | |||
|  | const VK_KEY KeyMsg::I = 'I'; | |||
|  | const VK_KEY KeyMsg::J = 'J'; | |||
|  | const VK_KEY KeyMsg::K = 'K'; | |||
|  | const VK_KEY KeyMsg::L = 'L'; | |||
|  | const VK_KEY KeyMsg::M = 'M'; | |||
|  | const VK_KEY KeyMsg::N = 'N'; | |||
|  | const VK_KEY KeyMsg::O = 'O'; | |||
|  | const VK_KEY KeyMsg::P = 'P'; | |||
|  | const VK_KEY KeyMsg::Q = 'Q'; | |||
|  | const VK_KEY KeyMsg::R = 'R'; | |||
|  | const VK_KEY KeyMsg::S = 'S'; | |||
|  | const VK_KEY KeyMsg::T = 'T'; | |||
|  | const VK_KEY KeyMsg::U = 'U'; | |||
|  | const VK_KEY KeyMsg::V = 'V'; | |||
|  | const VK_KEY KeyMsg::W = 'W'; | |||
|  | const VK_KEY KeyMsg::X = 'X'; | |||
|  | const VK_KEY KeyMsg::Y = 'Y'; | |||
|  | const VK_KEY KeyMsg::Z = 'Z'; | |||
|  | const VK_KEY KeyMsg::NUM_0 = '0'; | |||
|  | const VK_KEY KeyMsg::NUM_1 = '1'; | |||
|  | const VK_KEY KeyMsg::NUM_2 = '2'; | |||
|  | const VK_KEY KeyMsg::NUM_3 = '3'; | |||
|  | const VK_KEY KeyMsg::NUM_4 = '4'; | |||
|  | const VK_KEY KeyMsg::NUM_5 = '5'; | |||
|  | const VK_KEY KeyMsg::NUM_6 = '6'; | |||
|  | const VK_KEY KeyMsg::NUM_7 = '7'; | |||
|  | const VK_KEY KeyMsg::NUM_8 = '8'; | |||
|  | const VK_KEY KeyMsg::NUM_9 = '9'; | |||
|  | const VK_KEY KeyMsg::NUMPAD_0 = VK_NUMPAD0; | |||
|  | const VK_KEY KeyMsg::NUMPAD_1 = VK_NUMPAD1; | |||
|  | const VK_KEY KeyMsg::NUMPAD_2 = VK_NUMPAD2; | |||
|  | const VK_KEY KeyMsg::NUMPAD_3 = VK_NUMPAD3; | |||
|  | const VK_KEY KeyMsg::NUMPAD_4 = VK_NUMPAD4; | |||
|  | const VK_KEY KeyMsg::NUMPAD_5 = VK_NUMPAD5; | |||
|  | const VK_KEY KeyMsg::NUMPAD_6 = VK_NUMPAD6; | |||
|  | const VK_KEY KeyMsg::NUMPAD_7 = VK_NUMPAD7; | |||
|  | const VK_KEY KeyMsg::NUMPAD_8 = VK_NUMPAD8; | |||
|  | const VK_KEY KeyMsg::NUMPAD_9 = VK_NUMPAD9; | |||
|  | const VK_KEY KeyMsg::Enter = VK_RETURN; | |||
|  | const VK_KEY KeyMsg::Space = VK_SPACE; | |||
|  | const VK_KEY KeyMsg::Decimal = VK_DECIMAL; | |||
|  | const VK_KEY KeyMsg::Ctrl = VK_CONTROL; | |||
|  | const VK_KEY KeyMsg::LCtrl = VK_LCONTROL; | |||
|  | const VK_KEY KeyMsg::RCtrl = VK_RCONTROL; | |||
|  | const VK_KEY KeyMsg::Shift = VK_SHIFT; | |||
|  | const VK_KEY KeyMsg::LShift = VK_LSHIFT; | |||
|  | const VK_KEY KeyMsg::RShift = VK_RSHIFT; | |||
|  | const VK_KEY KeyMsg::Up = VK_UP; | |||
|  | const VK_KEY KeyMsg::Down = VK_DOWN; | |||
|  | const VK_KEY KeyMsg::Left = VK_LEFT; | |||
|  | const VK_KEY KeyMsg::Right = VK_RIGHT; | |||
|  | const VK_KEY KeyMsg::Esc = VK_ESCAPE; | |||
|  | const VK_KEY KeyMsg::F1 = VK_F1; | |||
|  | const VK_KEY KeyMsg::F2 = VK_F2; | |||
|  | const VK_KEY KeyMsg::F3 = VK_F3; | |||
|  | const VK_KEY KeyMsg::F4 = VK_F4; | |||
|  | const VK_KEY KeyMsg::F5 = VK_F5; | |||
|  | const VK_KEY KeyMsg::F6 = VK_F6; | |||
|  | const VK_KEY KeyMsg::F7 = VK_F7; | |||
|  | const VK_KEY KeyMsg::F8 = VK_F8; | |||
|  | const VK_KEY KeyMsg::F9 = VK_F9; | |||
|  | const VK_KEY KeyMsg::F10 = VK_F10; | |||
|  | const VK_KEY KeyMsg::F11 = VK_F11; | |||
|  | const VK_KEY KeyMsg::F12 = VK_F12; | |||
|  | 
 | |||
|  | KeyMsg::KeyMsg(tstring name, const KEY_CALLBACK & callback) | |||
|  | { | |||
|  | 	m_sName = name; | |||
|  | 	m_callback = callback; | |||
|  | } | |||
|  | 
 | |||
|  | KeyMsg::~KeyMsg() | |||
|  | { | |||
|  | } | |||
|  | 
 | |||
|  | void KeyMsg::onKbHit(VK_KEY key) | |||
|  | { | |||
|  | 	m_callback(key); | |||
|  | } | |||
|  | 
 | |||
|  | void KeyMsg::__exec() | |||
|  | { | |||
|  | 	if (_kbhit())								// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
 | |||
|  | 	{ | |||
|  | 		for (VK_KEY key : s_vKeys)				// ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
 | |||
|  | 		{ | |||
|  | 			if (GetAsyncKeyState(key) & 0x8000)	// <20>жϸü<CFB8><C3BC>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 			{ | |||
|  | 				for (auto k : s_vKeyMsg)		// <20>ַ<EFBFBD><D6B7>ð<EFBFBD><C3B0><EFBFBD><EFBFBD><EFBFBD>Ϣ
 | |||
|  | 				{ | |||
|  | 					k->onKbHit(key);			// ִ<>а<EFBFBD><D0B0><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 				} | |||
|  | 			} | |||
|  | 		} | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | void KeyMsg::addListener(tstring name, const KEY_CALLBACK & callback) | |||
|  | { | |||
|  | 	// <20><><EFBFBD><EFBFBD><EFBFBD>µļ<C2B5><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 	auto key = new KeyMsg(name, callback); | |||
|  | 	// <20><><EFBFBD><EFBFBD><EFBFBD>µİ<C2B5><C4B0><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 	s_vKeyMsg.push_back(key); | |||
|  | } | |||
|  | 
 | |||
|  | bool easy2d::KeyMsg::delListener(tstring name) | |||
|  | { | |||
|  | 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 	std::vector<KeyMsg*>::iterator iter; | |||
|  | 	// ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 	for (iter = s_vKeyMsg.begin(); iter != s_vKeyMsg.end(); iter++) | |||
|  | 	{ | |||
|  | 		// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD>Ƶļ<C6B5><C4BC><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 		if ((*iter)->m_sName == name) | |||
|  | 		{ | |||
|  | 			// ɾ<><C9BE><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1>
 | |||
|  | 			delete (*iter); | |||
|  | 			s_vKeyMsg.erase(iter); | |||
|  | 			return true; | |||
|  | 		} | |||
|  | 	} | |||
|  | 	// <20><>δ<EFBFBD>ҵ<EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD>Ƶļ<C6B5><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> false
 | |||
|  | 	return false; | |||
|  | } | |||
|  | 
 | |||
|  | void easy2d::KeyMsg::clearAllListener() | |||
|  | { | |||
|  | 	// ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 	for (auto t : s_vKeyMsg) | |||
|  | 	{ | |||
|  | 		delete t; | |||
|  | 	} | |||
|  | 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 	s_vKeyMsg.clear(); | |||
|  | } | |||
|  | 
 | |||
|  | bool KeyMsg::isKeyDown(VK_KEY key) | |||
|  | { | |||
|  | 	// <20><>ȡ key <20>İ<EFBFBD><C4B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 	return (GetAsyncKeyState(key) & 0x8000); | |||
|  | } |