///////////////////////////////////////////////////////////////////// // CInputSystem::InitializeSystem ///////////////////////////////////////////////////////////////////// bool CInputSystem::InitializeSystem(void *initStructure) { // -------------------------------------------------------------- // Get the initialization parameters if (initStructure == NULL) { return false; } SInputSystemInfoStructure *initParams = (SInputSystemInfoStructure*)initStructure; if (initParams->windowHandle == NULL) { return false; } if (initParams->instance == NULL) { return false; } // -------------------------------------------------------------- // Initialize DirectInput HRESULT hr = DirectInput8Create(initParams->instance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&diObject, NULL); if (FAILED(hr)) { return false; } inputSystemInfo.instance = initParams->instance; inputSystemInfo.windowHandle = initParams->windowHandle; // -------------------------------------------------------------- // Create input devices if (!CreateMouseDevice()) { return false; } if (!CreateKeyboardDevice()) { return false; } // -------------------------------------------------------------- // Acquire Devices if (!AcquireMouse()) { return false; } if (!AcquireKeyboard()) { return false; } return true; }
bool CInputSystem::ReadMouseState() { HRESULT hr; if (!mouseDevice) { return false; } //--------------------------------------------------------------- // Get Keyboard State hr = mouseDevice->GetDeviceState( sizeof(DIMOUSESTATE), (LPVOID)&mouseState ); if (SUCCEEDED(hr)) { return true; } // If Keyboard focus lost then reacquire if (hr==DIERR_INPUTLOST) { // Try again if (AcquireMouse()) { //------------------------------------------------------- // Get Keyboard State hr = mouseDevice->GetDeviceState( sizeof(DIMOUSESTATE), (LPVOID)&mouseState ); if (SUCCEEDED(hr)) { return true; } } } // TODO: Temp Debug Text std::cout << "Mouse Input Error: " << hr << std::endl; return false; }
bool Input::FilterMessage (HWND hwnd_, UINT uMsg_, WPARAM wParam_, LPARAM lParam_) { // Mouse button decoder table, from the lower 3 bits of the Windows message values static int anMouseButtons[] = { 2, 1, 1, 0, 3, 3, 0, 2 }; POINT pt = { GET_X_LPARAM(lParam_), GET_Y_LPARAM(lParam_) }; ClientToScreen(hwnd_, &pt); ScreenToClient(hwndCanvas, &pt); switch (uMsg_) { // Input language has changed - reinitialise input to pick up the new mappings case WM_INPUTLANGCHANGE: Init(); break; // Release the mouse and purge keyboard input on activation changes case WM_ACTIVATE: AcquireMouse(false); Purge(); break; // Release the mouse on entering the menu case WM_ENTERMENULOOP: AcquireMouse(false); Purge(); // needed to avoid Alt-X menu shortcuts from being seen break; case WM_MOUSEMOVE: { // If the GUI is active, pass the message to it if (GUI::IsActive()) { SendGuiMouseMessage(GM_MOUSEMOVE, &pt); break; } // Otherwise the SAM mouse must be active for it to be of interest else if (!fMouseActive) break; // Work out the relative movement since last time POINT ptCursor; GetCursorPos(&ptCursor); int nX = ptCursor.x - ptCentre.x, nY = ptCursor.y - ptCentre.y; // Any native movement? if (nX || nY) { Video::DisplayToSamSize(&nX, &nY); // Any SAM movement? if (nX || nY) { // Update the SAM mouse and re-centre the cursor pMouse->Move(nX, -nY); SetCursorPos(ptCentre.x, ptCentre.y); } } break; } case WM_LBUTTONDOWN: case WM_RBUTTONDOWN: case WM_MBUTTONDOWN: case WM_LBUTTONDBLCLK: { // The GUI gets first chance to process the message if (GUI::IsActive()) SendGuiMouseMessage(GM_BUTTONDOWN, &pt); // If the mouse is already active, pass on button presses else if (fMouseActive) pMouse->SetButton(anMouseButtons[uMsg_ & 0x7], true); // If the mouse interface is enabled and being read by something other than the ROM, a left-click acquires it // Otherwise a double-click is required to forcibly acquire it else if (GetOption(mouse) && ((uMsg_ == WM_LBUTTONDOWN && pMouse->IsActive()) || uMsg_ == WM_LBUTTONDBLCLK)) AcquireMouse(true); break; } case WM_LBUTTONUP: case WM_RBUTTONUP: case WM_MBUTTONUP: { // The GUI gets first chance to process the message if (GUI::IsActive()) SendGuiMouseMessage(GM_BUTTONUP, &pt); // Pass the button release through to the mouse module else if (fMouseActive) pMouse->SetButton(anMouseButtons[uMsg_ & 0x7], false); break; } case WM_MOUSEWHEEL: if (GUI::IsActive()) GUI::SendMessage(GM_MOUSEWHEEL, (GET_WHEEL_DELTA_WPARAM(wParam_) < 0) ? 1 : -1); break; case WM_CHAR: case WM_KEYDOWN: { if (!GUI::IsActive()) { // If escape is pressed, release mouse capture and stop any auto-typing if (wParam_ == VK_ESCAPE && GetOption(mouseesc)) { AcquireMouse(false); Keyin::Stop(); } // Ignore key repeats for non-GUI keys return !!(lParam_ & 0x40000000); } // Determine the current shift states int nMods = HM_NONE; if (GetKeyState(VK_SHIFT) < 0) nMods |= HM_SHIFT; if (GetKeyState(VK_CONTROL) < 0) nMods |= HM_CTRL; // Regular characters if (uMsg_ == WM_CHAR) { GUI::SendMessage(GM_CHAR, static_cast<int>(wParam_), nMods); break; } // Ctrl-letter/digit else if ((nMods & HM_CTRL) && (wParam_ >= 'A' && wParam_ <= 'Z' || wParam_ >= '0' && wParam_ <= '9')) GUI::SendMessage(GM_CHAR, int(wParam_), nMods); // Keypad digits else if (wParam_ >= VK_NUMPAD0 && wParam_ <= VK_NUMPAD9) GUI::SendMessage(GM_CHAR, HK_KP0 + static_cast<int>(wParam_)-VK_NUMPAD0, nMods); // Cursor keys + navigation cluster else if (wParam_ >= VK_PRIOR && wParam_ <= VK_DOWN) { static int an[] = { HK_PGUP, HK_PGDN, HK_END, HK_HOME, HK_LEFT, HK_UP, HK_RIGHT, HK_DOWN }; GUI::SendMessage(GM_CHAR, an[wParam_-VK_PRIOR], nMods); } // Delete else if (wParam_ == VK_DELETE) GUI::SendMessage(GM_CHAR, HK_DELETE, nMods); // Not processed here, but may come back through decoded as WM_CHAR else return true; return false; } } // Message not processed return false; }