// XDebugKeyboard.cpp // ////////////////////////////////////////////////////////////////////// #ifdef DEBUG_KEYBOARD #include "stdafx.h" #include "Input.h" #include #include #include #include "XDebugKeyboard.h" #include #pragma comment (lib, "xkbdd.lib") #ifdef _DEBUG static char THIS_FILE[] = __FILE__; #define DEBUG_CLIENTBLOCK new( _NORMAL_BLOCK, THIS_FILE, __LINE__) #define new DEBUG_CLIENTBLOCK #endif inline MapVirtualKey(int x, int y) { return x; } inline ToAscii(unsigned long vKeyCode, int k, unsigned char sKState[], unsigned short ascii[], int val ) { return vKeyCode; } namespace{ const DWORD g_dwNumToPort[] = {XDEVICE_PORT0, XDEVICE_PORT1, XDEVICE_PORT2, XDEVICE_PORT3}; } #define NumToPort(X) g_dwNumToPort[X] XAsciiKey CXDebugKeyboard::m_AsciiTable[256]; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// //XAsciiKey CXKeyboard::m_AsciiTable[256]; CXDebugKeyboard::CXDebugKeyboard() { memset(m_cKeysState, 0, sizeof(m_cKeysState)); memset(m_cOldKeysState, 0, sizeof(m_cOldKeysState)); memset(m_bKeys, 0, sizeof(m_bKeys)); m_hDebugKeyboard = NULL; m_pInput = 0; } CXDebugKeyboard::~CXDebugKeyboard() { } bool CXDebugKeyboard::Init(CInput * pInput, ILog *pLog) { char szBuffer[10240]; m_pInput = pInput; m_pLog=pLog; m_pLog->LogToFile("Initializing Debug Keyboard\n"); /* XDEVICE_PREALLOC_TYPE sDeviceTypes[] = { { XDEVICE_TYPE_DEBUG_KEYBOARD, 1 }, }; // Initialize all four gamepads and the debug keyboard XInitDevices(sizeof(sDeviceTypes) / sizeof(XDEVICE_PREALLOC_TYPE), sDeviceTypes); */ DWORD dwReturn; XINPUT_DEBUG_KEYQUEUE_PARAMETERS xdkp = { XINPUT_DEBUG_KEYQUEUE_FLAG_KEYDOWN | XINPUT_DEBUG_KEYQUEUE_FLAG_KEYUP, 25, 500, 50 }; // Init the debug keyboard dwReturn = XInputDebugInitKeyboardQueue(&xdkp); assert(dwReturn == ERROR_SUCCESS); DWORD dwDeviceMask = XGetDevices( XDEVICE_TYPE_DEBUG_KEYBOARD ); // Open the devices for( DWORD i=0; i < XGetPortCount(); i++ ) { if( dwDeviceMask & (1<> 8 ); else if ( nResult == 1 ) m_AsciiTable[k].lc[0] =(char) ascii[0]; //::OutputDebugString(m_AsciiTable[k].lc); ::OutputDebugString("\n"); ascii[0] = ascii[1] = 0; sKState[VK_SHIFT] = 0x80; nResult=ToAscii( vKeyCode, k, sKState, ascii, 0 ); if ( nResult == 2 ) m_AsciiTable[k].uc[0] =(char) ascii[1] ? ascii[1] : ( ascii[0] >> 8 ); else if ( nResult == 1 ) m_AsciiTable[k].uc[0] =(char) ascii[0]; //::OutputDebugString(m_AsciiTable[k].uc); ::OutputDebugString("\n"); } } ////////////////////////////////////////////////////////////////////////// void CXDebugKeyboard::Update() { //BOOL bReturn; DWORD dwInsertions = 0, dwRemovals = 0; unsigned int i; char szBuffer[10240]; DWORD dwReturn; // Query the debug keyboard XINPUT_DEBUG_KEYSTROKE sKeyStroke; // Query changes XGetDeviceChanges(XDEVICE_TYPE_DEBUG_KEYBOARD, &dwInsertions, &dwRemovals); // Detect keyboard insertions for (i=0; i<4; i++) { if (dwInsertions & (1 << i)) { // A controller was inserted sprintf(szBuffer, "INPUT: Keyboard %i inserted\n", i); OutputDebugString(szBuffer); // Open the keyboard on the port XINPUT_POLLING_PARAMETERS pollValues; pollValues.fAutoPoll = TRUE; pollValues.fInterruptOut = TRUE; pollValues.bInputInterval = 32; pollValues.bOutputInterval = 32; pollValues.ReservedMBZ1 = 0; pollValues.ReservedMBZ2 = 0; m_hDebugKeyboard = XInputOpen(XDEVICE_TYPE_DEBUG_KEYBOARD, NumToPort(i), XDEVICE_NO_SLOT, &pollValues); if (m_hDebugKeyboard != NULL) { sprintf(szBuffer, "INPUT: Keyboard %i successfully opened\n", i); OutputDebugString(szBuffer); } else { sprintf(szBuffer, "INPUT: ERROR: Can't open keyboard %i\n", i); OutputDebugString(szBuffer); } } } // Detect keyboard removal for (i=0; i<4; i++) { if (dwRemovals & (1 << i)) { // A keyboard was removed sprintf(szBuffer, "INPUT: Keyboard %i removed\n", i); OutputDebugString(szBuffer); // Close the controller XInputClose(m_hDebugKeyboard); m_hDebugKeyboard = NULL; } } // Poll for input when we got a keyboard connected and initializd if (m_hDebugKeyboard) { memcpy(m_cOldKeysState,m_cKeysState,sizeof(m_cOldKeysState)); // Query the keyboard state dwReturn = XInputDebugGetKeystroke(&sKeyStroke); // Error ? if (dwReturn == ERROR_SUCCESS) { // We got a key up or key down event ? DWORD dik =-1; switch(sKeyStroke.VirtualKey) { case VK_TAB :dik = XKEY_TAB ;break; case VK_BACK :dik = XKEY_BACKSPACE ;break; case VK_RETURN :dik = XKEY_RETURN ;break; case VK_SHIFT :dik = XKEY_SHIFT ;break; case VK_CONTROL :dik = XKEY_CONTROL ;break; case VK_PAUSE :dik = XKEY_PAUSE ;break; case VK_ESCAPE :dik = XKEY_ESCAPE ;break; case VK_SPACE :dik = XKEY_SPACE ;break; case VK_END :dik = XKEY_END ;break; case VK_HOME :dik = XKEY_HOME ;break; case VK_LEFT :dik = XKEY_LEFT ;break; case VK_UP :dik = XKEY_UP ;break; case VK_RIGHT :dik = XKEY_RIGHT ;break; case VK_DOWN :dik = XKEY_DOWN ;break; case VK_PRINT :dik = XKEY_PRINT ;break; case VK_INSERT :dik = XKEY_INSERT ;break; case VK_DELETE :dik = XKEY_DELETE ;break; case VK_HELP :dik = XKEY_HELP ;break; //case VK_LALT :dik = XKEY_LALT ;break; //case VK_RALT :dik = XKEY_RALT ;break; case VK_LSHIFT :dik = XKEY_LSHIFT ;break; case VK_RSHIFT :dik = XKEY_RSHIFT ;break; case VK_LCONTROL :dik = XKEY_LCONTROL ;break; case VK_RCONTROL :dik = XKEY_RCONTROL ;break; case VK_NUMPAD0 : dik = XKEY_NUMPAD0 ;break; case VK_NUMPAD1 : dik = XKEY_NUMPAD1 ;break; case VK_NUMPAD2 : dik = XKEY_NUMPAD2 ;break; case VK_NUMPAD3 : dik = XKEY_NUMPAD3 ;break; case VK_NUMPAD4 : dik = XKEY_NUMPAD4 ;break; case VK_NUMPAD5 : dik = XKEY_NUMPAD5 ;break; case VK_NUMPAD6 : dik = XKEY_NUMPAD6 ;break; case VK_NUMPAD7 : dik = XKEY_NUMPAD7 ;break; case VK_NUMPAD8 : dik = XKEY_NUMPAD8 ;break; case VK_NUMPAD9 : dik = XKEY_NUMPAD9 ;break; case VK_MULTIPLY : dik = XKEY_MULTIPLY ;break; case VK_ADD : dik = XKEY_ADD ;break; case VK_SEPARATOR : dik = XKEY_SEPARATOR ;break; case VK_SUBTRACT : dik = XKEY_SUBTRACT ;break; case VK_DECIMAL : dik = XKEY_DECIMAL ;break; case VK_DIVIDE : dik = XKEY_DIVIDE ;break; case VK_F1 : dik = XKEY_F1 ;break; case VK_F2 : dik = XKEY_F2 ;break; case VK_F3 : dik = XKEY_F3 ;break; case VK_F4 : dik = XKEY_F4 ;break; case VK_F5 : dik = XKEY_F5 ;break; case VK_F6 : dik = XKEY_F6 ;break; case VK_F7 : dik = XKEY_F7 ;break; case VK_F8 : dik = XKEY_F8 ;break; case VK_F9 : dik = XKEY_F9 ;break; case VK_F10 : dik = XKEY_F10 ;break; case VK_F11 : dik = XKEY_F11 ;break; case VK_F12 : dik = XKEY_F12 ;break; case VK_F13 : dik = XKEY_F13 ;break; case VK_F14 : dik = XKEY_F14 ;break; case VK_F15 : dik = XKEY_F15 ;break; case VK_F16 : dik = XKEY_F16 ;break; case VK_F17 : dik = XKEY_F17 ;break; case VK_F18 : dik = XKEY_F18 ;break; case VK_F19 : dik = XKEY_F19 ;break; case VK_F20 : dik = XKEY_F20 ;break; case VK_F21 : dik = XKEY_F21 ;break; case VK_F22 : dik = XKEY_F22 ;break; case VK_F23 : dik = XKEY_F23 ;break; case VK_F24 : dik = XKEY_F24 ;break; } if(dik != -1) m_cKeysState[dik] = (sKeyStroke.Flags & XINPUT_DEBUG_KEYSTROKE_FLAG_KEYUP)? false: true; dik = 0; if('a' <= sKeyStroke.Ascii && sKeyStroke.Ascii <='z') dik = XKEY_A + sKeyStroke.Ascii - 'a'; if('A' <= sKeyStroke.Ascii && sKeyStroke.Ascii <='Z') dik = XKEY_A + sKeyStroke.Ascii - 'A'; if('0' <= sKeyStroke.Ascii && sKeyStroke.Ascii <='9') dik = XKEY_0 + sKeyStroke.Ascii - '0'; switch(sKeyStroke.Ascii) { case '`' :dik = XKEY_TILDE ;break; case '=' :dik = XKEY_EQUALS ;break; case '[' :dik = XKEY_LBRACKET ;break; case ']' :dik = XKEY_RBRACKET ;break; case '\\' :dik = XKEY_BACKSLASH ;break; case ';' :dik = XKEY_SEMICOLON ;break; case '\'' :dik = XKEY_APOSTROPHE ;break; case ',' :dik = XKEY_COMMA ;break; case '-' :dik = XKEY_MINUS ;break; case '.' :dik = XKEY_DECIMAL ;break; case '/' :dik = XKEY_SLASH ;break; case '¬' :dik = XKEY_TILDE ;break; case '+' :dik = XKEY_EQUALS ;break; case '{' :dik = XKEY_LBRACKET ;break; case '}' :dik = XKEY_RBRACKET ;break; case '|' :dik = XKEY_BACKSLASH ;break; case ':' :dik = XKEY_SEMICOLON ;break; case '"' :dik = XKEY_APOSTROPHE ;break; case '<' :dik = XKEY_COMMA ;break; case '_' :dik = XKEY_MINUS ;break; case '>' :dik = XKEY_DECIMAL ;break; case '?' :dik = XKEY_SLASH ;break; case ')' :dik = XKEY_0 ;break; case '!' :dik = XKEY_1 ;break; case '@' :dik = XKEY_2 ;break; case '#' :dik = XKEY_3 ;break; case '$' :dik = XKEY_4 ;break; case '%' :dik = XKEY_5 ;break; case '^' :dik = XKEY_6 ;break; case '&' :dik = XKEY_7 ;break; case '*' :dik = XKEY_8 ;break; case '(' :dik = XKEY_9 ;break; } if(dik!=0) m_cKeysState[dik] = (sKeyStroke.Flags & XINPUT_DEBUG_KEYSTROKE_FLAG_KEYUP)? false: true; } else { // We got no success, and the keyboard buffer is not empty ? if (dwReturn != ERROR_HANDLE_EOF) OutputDebugString("INPUT: Error while reading from debug keyboard"); } } } ////////////////////////////////////////////////////////////////////////// void CXDebugKeyboard::ShutDown() { if (m_hDebugKeyboard) { XInputClose(m_hDebugKeyboard); m_hDebugKeyboard = NULL; } } ////////////////////////////////////////////////////////////////////////// void CXDebugKeyboard::SetKey(int p_key, int value) { } ////////////////////////////////////////////////////////////////////////// void CXDebugKeyboard::SetPrevKey(int p_key, int value) { } ////////////////////////////////////////////////////////////////////////// bool CXDebugKeyboard::KeyDown(int p_key) { return ((m_cKeysState[p_key]) != 0); } ////////////////////////////////////////////////////////////////////////// bool CXDebugKeyboard::KeyPressed(int p_key) { if(p_key == XKEY_MINUS) { if(m_cKeysState[p_key]!=0) { int ga=1; } int gamedev=1; } return ((m_cKeysState[p_key]) != 0 && m_cOldKeysState[p_key] == 0); /* unsigned char cDik=XKEY2DIK(p_key); #ifndef PS2 if(((m_cKeysState[cDik] & 0x80) != 0) && ((m_cOldKeysState[cDik] & 0x80) == 0)) return true; else return false; #else if(cDik<0) return false; //PS2 SPECIFIC CODE if(ASCIPRESSED[cDik]&&!ASCIPRESSEDOLD[cDik]) return true; else return false; #endif */ return false; } ////////////////////////////////////////////////////////////////////////// bool CXDebugKeyboard::KeyReleased(int p_key) { return ((m_cKeysState[p_key] ) == 0) && ((m_cOldKeysState[p_key] ) != 0); /* unsigned char cDik=XKEY2DIK(p_key); #ifndef PS2 return ((m_cKeysState[cDik] & 0x80) == 0) && ((m_cOldKeysState[cDik] & 0x80) != 0); #else if(cDik<0) return false; //PS2 SPECIFIC CODE if(!ASCIPRESSED[cDik]&&ASCIPRESSEDOLD[cDik]) return true; else return false; #endif */ return false; } ////////////////////////////////////////////////////////////////////////// void CXDebugKeyboard::ClearKey(int p_key) { m_cOldKeysState[p_key]=m_cKeysState[p_key]; m_cKeysState[p_key] = 0; /* unsigned char cDik=XKEY2DIK(p_key); #ifndef PS2 m_cOldKeysState[cDik]=m_cKeysState[cDik]; m_cKeysState[cDik]=NULL; #else //PS2 SPECIFIC CODE if(cDik<0) return; ASCIPRESSEDOLD[cDik]=ASCIPRESSED[cDik]; ASCIPRESSED[cDik]=0; #endif */ } ////////////////////////////////////////////////////////////////////////// int CXDebugKeyboard::GetKeyPressedCode() { for (int k=0;k<256;k++) { int nXKey=DIK2XKEY(k); if(nXKey==XKEY_NULL) continue; if (KeyPressed(nXKey)) return nXKey; } return -1; } ////////////////////////////////////////////////////////////////////////// const char *CXDebugKeyboard::GetKeyPressedName() { int key=GetKeyPressedCode(); if (key==-1) return (NULL); return m_pInput->GetKeyName(key); } ////////////////////////////////////////////////////////////////////////// int CXDebugKeyboard::GetKeyDownCode() { for (int k=0;k<256;k++) if (KeyDown(DIK2XKEY(k))) return DIK2XKEY(k); return -1; } ////////////////////////////////////////////////////////////////////////// const char *CXDebugKeyboard::GetKeyDownName() { int key=GetKeyDownCode(); if (key==-1) return (NULL); return m_pInput->GetKeyName(key); } ////////////////////////////////////////////////////////////////////////// const char *CXDebugKeyboard::EnumerateKey(int nKeyCode) { return m_pInput->GetKeyName(nKeyCode); } ////////////////////////////////////////////////////////////////////////// void CXDebugKeyboard::WaitForKey() { CryError( " CXKeyboard::WaitForKey() invalid function call" ); } ////////////////////////////////////////////////////////////////////////// void CXDebugKeyboard::ClearKeyState() { memset(m_cKeysState,0,sizeof(m_cKeysState)); } #endif // DEBUG_KEYBOARD