Refactor sendKeyboard functions to new funcs, propogate constness

The sendKeyboardBackspace function was used with a non-const parameter
throughout the entire inheritance hierachy. Const has been added to
every parameter for the reasons you add const.
This commit is contained in:
Vasily
2019-01-12 19:14:56 +01:00
parent 73f9766de7
commit faa6ccc580
7 changed files with 49 additions and 31 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ public:
virtual void sendKeyboardInput( QString input ) = 0; virtual void sendKeyboardInput( QString input ) = 0;
virtual void sendKeyboardEnter() = 0; virtual void sendKeyboardEnter() = 0;
virtual void sendKeyboardBackspace( int count ) = 0; virtual void sendKeyboardBackspace( const int count ) = 0;
virtual void sendKeyboardAltTab() = 0; virtual void sendKeyboardAltTab() = 0;
}; };
@@ -83,7 +83,7 @@ void UtilitiesTabController::sendKeyboardAltTab()
keyboardInput->sendKeyboardAltTab(); keyboardInput->sendKeyboardAltTab();
} }
void UtilitiesTabController::sendKeyboardBackspace( int count ) void UtilitiesTabController::sendKeyboardBackspace( const int count )
{ {
keyboardInput->sendKeyboardBackspace( count ); keyboardInput->sendKeyboardBackspace( count );
} }
+1 -1
View File
@@ -69,7 +69,7 @@ public slots:
void sendKeyboardInput( QString input ); void sendKeyboardInput( QString input );
void sendKeyboardEnter(); void sendKeyboardEnter();
void sendKeyboardAltTab(); void sendKeyboardAltTab();
void sendKeyboardBackspace( int count ); void sendKeyboardBackspace( const int count );
void setAlarmEnabled( bool enabled, bool notify = true ); void setAlarmEnabled( bool enabled, bool notify = true );
void setAlarmIsModal( bool modal, bool notify = true ); void setAlarmIsModal( bool modal, bool notify = true );
@@ -17,7 +17,7 @@ void KeyboardInputDummy::sendKeyboardEnter()
// noop // noop
} }
void KeyboardInputDummy::sendKeyboardBackspace( int count ) void KeyboardInputDummy::sendKeyboardBackspace( const int count )
{ {
// noop // noop
UNREFERENCED_PARAMETER( count ); UNREFERENCED_PARAMETER( count );
@@ -11,7 +11,7 @@ class KeyboardInputDummy : public KeyboardInput
public: public:
virtual void sendKeyboardInput( QString input ) override; virtual void sendKeyboardInput( QString input ) override;
virtual void sendKeyboardEnter() override; virtual void sendKeyboardEnter() override;
virtual void sendKeyboardBackspace( int count ) override; virtual void sendKeyboardBackspace( const int count ) override;
virtual void sendKeyboardAltTab() override; virtual void sendKeyboardAltTab() override;
}; };
@@ -15,6 +15,9 @@ enum class KeyStatus
Down, Down,
}; };
/*!
Fills out an INPUT struct ip with scanCode and keyup status.
*/
void fillKiStruct( INPUT& ip, WORD scanCode, bool keyup ) void fillKiStruct( INPUT& ip, WORD scanCode, bool keyup )
{ {
ip.type = INPUT_KEYBOARD; ip.type = INPUT_KEYBOARD;
@@ -32,6 +35,12 @@ void fillKiStruct( INPUT& ip, WORD scanCode, bool keyup )
ip.ki.time = 0; 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 ) void sendKeyboardInputRaw( const int inputCount, const LPINPUT input )
{ {
const auto success const auto success
@@ -190,44 +199,53 @@ void KeyboardInputWindows::sendKeyboardInput( QString input )
} }
} }
/*!
Sends a single Enter keypress to Windows.
*/
void KeyboardInputWindows::sendKeyboardEnter() void KeyboardInputWindows::sendKeyboardEnter()
{ {
LPINPUT ips = new INPUT[2]; sendKeyPressAndRelease( VK_RETURN );
fillKiStruct( ips[0], VK_RETURN, false );
fillKiStruct( ips[1], VK_RETURN, true );
sendKeyboardInputRaw( 2, ips );
delete[] ips;
} }
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. return;
LPINPUT ips = new INPUT[static_cast<unsigned int>( count ) * 2]; }
for ( int i = 0; i < count; i++ )
{
fillKiStruct( ips[2 * i], VK_BACK, false );
fillKiStruct( ips[2 * i + 1], VK_BACK, true );
}
sendKeyboardInputRaw( count * 2, ips ); for ( int presses = 0; presses < count; ++presses )
delete[] ips; {
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() void KeyboardInputWindows::sendKeyboardAltTab()
{ {
LPINPUT ips = new INPUT[4]; // VK_MENU is Alt.
// VK_MENU is alt const auto pressAlt = createInputStruct( VK_MENU, KeyStatus::Down );
fillKiStruct( ips[0], VK_MENU, false ); const auto pressTab = createInputStruct( VK_TAB, KeyStatus::Down );
fillKiStruct( ips[1], VK_TAB, false ); const auto releaseAlt = createInputStruct( VK_MENU, KeyStatus::Up );
fillKiStruct( ips[2], VK_TAB, true ); const auto releaseTab = createInputStruct( VK_TAB, KeyStatus::Up );
fillKiStruct( ips[3], VK_MENU, true );
sendKeyboardInputRaw( 4, ips ); constexpr auto numberOfActions = 4;
delete[] ips;
INPUT actions[numberOfActions]
= { pressAlt, pressTab, releaseAlt, releaseTab };
sendKeyboardInputRaw( numberOfActions, actions );
} }
} // namespace advsettings } // namespace advsettings
@@ -11,7 +11,7 @@ class KeyboardInputWindows : public KeyboardInput
public: public:
virtual void sendKeyboardInput( QString input ) override; virtual void sendKeyboardInput( QString input ) override;
virtual void sendKeyboardEnter() override; virtual void sendKeyboardEnter() override;
virtual void sendKeyboardBackspace( int count ) override; virtual void sendKeyboardBackspace( const int count ) override;
virtual void sendKeyboardAltTab() override; virtual void sendKeyboardAltTab() override;
}; };