diff --git a/src/tabcontrollers/KeyboardInput.h b/src/tabcontrollers/KeyboardInput.h index f660eb3..44b9c7f 100644 --- a/src/tabcontrollers/KeyboardInput.h +++ b/src/tabcontrollers/KeyboardInput.h @@ -15,7 +15,7 @@ public: virtual void sendKeyboardInput( QString input ) = 0; virtual void sendKeyboardEnter() = 0; - virtual void sendKeyboardBackspace( int count ) = 0; + virtual void sendKeyboardBackspace( const int count ) = 0; virtual void sendKeyboardAltTab() = 0; }; diff --git a/src/tabcontrollers/UtilitiesTabController.cpp b/src/tabcontrollers/UtilitiesTabController.cpp index 0684b46..72a71ae 100644 --- a/src/tabcontrollers/UtilitiesTabController.cpp +++ b/src/tabcontrollers/UtilitiesTabController.cpp @@ -83,7 +83,7 @@ void UtilitiesTabController::sendKeyboardAltTab() keyboardInput->sendKeyboardAltTab(); } -void UtilitiesTabController::sendKeyboardBackspace( int count ) +void UtilitiesTabController::sendKeyboardBackspace( const int count ) { keyboardInput->sendKeyboardBackspace( count ); } diff --git a/src/tabcontrollers/UtilitiesTabController.h b/src/tabcontrollers/UtilitiesTabController.h index 375e2e4..17b826b 100644 --- a/src/tabcontrollers/UtilitiesTabController.h +++ b/src/tabcontrollers/UtilitiesTabController.h @@ -69,7 +69,7 @@ public slots: void sendKeyboardInput( QString input ); void sendKeyboardEnter(); void sendKeyboardAltTab(); - void sendKeyboardBackspace( int count ); + void sendKeyboardBackspace( const int count ); void setAlarmEnabled( bool enabled, bool notify = true ); void setAlarmIsModal( bool modal, bool notify = true ); diff --git a/src/tabcontrollers/keyboardinput/KeyboardInputDummy.cpp b/src/tabcontrollers/keyboardinput/KeyboardInputDummy.cpp index 4bb4aea..7cd8f6d 100644 --- a/src/tabcontrollers/keyboardinput/KeyboardInputDummy.cpp +++ b/src/tabcontrollers/keyboardinput/KeyboardInputDummy.cpp @@ -17,7 +17,7 @@ void KeyboardInputDummy::sendKeyboardEnter() // noop } -void KeyboardInputDummy::sendKeyboardBackspace( int count ) +void KeyboardInputDummy::sendKeyboardBackspace( const int count ) { // noop UNREFERENCED_PARAMETER( count ); diff --git a/src/tabcontrollers/keyboardinput/KeyboardInputDummy.h b/src/tabcontrollers/keyboardinput/KeyboardInputDummy.h index 66e5fa5..fb9df57 100644 --- a/src/tabcontrollers/keyboardinput/KeyboardInputDummy.h +++ b/src/tabcontrollers/keyboardinput/KeyboardInputDummy.h @@ -11,7 +11,7 @@ class KeyboardInputDummy : public KeyboardInput public: virtual void sendKeyboardInput( QString input ) override; virtual void sendKeyboardEnter() override; - virtual void sendKeyboardBackspace( int count ) override; + virtual void sendKeyboardBackspace( const int count ) override; virtual void sendKeyboardAltTab() override; }; diff --git a/src/tabcontrollers/keyboardinput/KeyboardInputWindows.cpp b/src/tabcontrollers/keyboardinput/KeyboardInputWindows.cpp index 07fa32e..b0e37a5 100644 --- a/src/tabcontrollers/keyboardinput/KeyboardInputWindows.cpp +++ b/src/tabcontrollers/keyboardinput/KeyboardInputWindows.cpp @@ -15,6 +15,9 @@ enum class KeyStatus Down, }; +/*! +Fills out an INPUT struct ip with scanCode and keyup status. +*/ void fillKiStruct( INPUT& ip, WORD scanCode, bool keyup ) { ip.type = INPUT_KEYBOARD; @@ -32,6 +35,12 @@ void fillKiStruct( INPUT& ip, WORD scanCode, bool keyup ) ip.ki.time = 0; }; +/*! +Calls SendInput on an INPUT struct and has associated error handling. + +inputCount can't be deduced from input because of implicit conversions from +arrays to pointers when passed as arguments. +*/ void sendKeyboardInputRaw( const int inputCount, const LPINPUT input ) { const auto success @@ -190,44 +199,53 @@ void KeyboardInputWindows::sendKeyboardInput( QString input ) } } +/*! +Sends a single Enter keypress to Windows. +*/ void KeyboardInputWindows::sendKeyboardEnter() { - LPINPUT ips = new INPUT[2]; - fillKiStruct( ips[0], VK_RETURN, false ); - fillKiStruct( ips[1], VK_RETURN, true ); - - sendKeyboardInputRaw( 2, ips ); - delete[] ips; + sendKeyPressAndRelease( VK_RETURN ); } -void KeyboardInputWindows::sendKeyboardBackspace( int count ) +/*! +Sends count amount of backspaces to Windows. count of 0 or below will do +nothing. +*/ +void KeyboardInputWindows::sendKeyboardBackspace( const int count ) { - if ( count > 0 ) + // We can only send a positive amount of key presses. + if ( count <= 0 ) { - // We ensure that count is nonnegative, therefore safe cast. - LPINPUT ips = new INPUT[static_cast( count ) * 2]; - for ( int i = 0; i < count; i++ ) - { - fillKiStruct( ips[2 * i], VK_BACK, false ); - fillKiStruct( ips[2 * i + 1], VK_BACK, true ); - } + return; + } - sendKeyboardInputRaw( count * 2, ips ); - delete[] ips; + for ( int presses = 0; presses < count; ++presses ) + { + sendKeyPressAndRelease( VK_BACK ); } } +/*! +Sends an Alt + Tab combination to Windows. + +Used for tabbing out of game windows blocking other programs on desktop while in +VR. At least one use case was tabbing out of a fullscreen game to be able to use +a music player. +*/ void KeyboardInputWindows::sendKeyboardAltTab() { - LPINPUT ips = new INPUT[4]; - // VK_MENU is alt - fillKiStruct( ips[0], VK_MENU, false ); - fillKiStruct( ips[1], VK_TAB, false ); - fillKiStruct( ips[2], VK_TAB, true ); - fillKiStruct( ips[3], VK_MENU, true ); + // VK_MENU is Alt. + const auto pressAlt = createInputStruct( VK_MENU, KeyStatus::Down ); + const auto pressTab = createInputStruct( VK_TAB, KeyStatus::Down ); + const auto releaseAlt = createInputStruct( VK_MENU, KeyStatus::Up ); + const auto releaseTab = createInputStruct( VK_TAB, KeyStatus::Up ); - sendKeyboardInputRaw( 4, ips ); - delete[] ips; + constexpr auto numberOfActions = 4; + + INPUT actions[numberOfActions] + = { pressAlt, pressTab, releaseAlt, releaseTab }; + + sendKeyboardInputRaw( numberOfActions, actions ); } } // namespace advsettings diff --git a/src/tabcontrollers/keyboardinput/KeyboardInputWindows.h b/src/tabcontrollers/keyboardinput/KeyboardInputWindows.h index e376703..380309c 100644 --- a/src/tabcontrollers/keyboardinput/KeyboardInputWindows.h +++ b/src/tabcontrollers/keyboardinput/KeyboardInputWindows.h @@ -11,7 +11,7 @@ class KeyboardInputWindows : public KeyboardInput public: virtual void sendKeyboardInput( QString input ) override; virtual void sendKeyboardEnter() override; - virtual void sendKeyboardBackspace( int count ) override; + virtual void sendKeyboardBackspace( const int count ) override; virtual void sendKeyboardAltTab() override; };