Merge pull request #9 from OpenVR-Advanced-Settings/keyboard-alt-tab

Add option to send Alt-Tab as keyboard input
This commit is contained in:
username223
2018-12-26 18:34:11 +01:00
committed by GitHub
8 changed files with 62 additions and 49 deletions
+12 -4
View File
@@ -45,7 +45,7 @@ MyStackViewPage {
MyPushButton { MyPushButton {
text: "Keyboard Input" text: "Keyboard Input"
Layout.preferredWidth: 250 Layout.preferredWidth: 200
onClicked: { onClicked: {
OverlayController.showKeyboard("", 601) OverlayController.showKeyboard("", 601)
} }
@@ -61,15 +61,23 @@ MyStackViewPage {
MyPushButton { MyPushButton {
text: "Enter" text: "Enter"
Layout.preferredWidth: 250 Layout.preferredWidth: 200
onClicked: { onClicked: {
UtilitiesTabController.sendKeyboardEnter() UtilitiesTabController.sendKeyboardEnter()
} }
} }
MyPushButton {
text: "Alt-Tab"
Layout.preferredWidth: 200
onClicked: {
UtilitiesTabController.sendKeyboardAltTab()
}
}
MyPushButton { MyPushButton {
text: "Backspace" text: "Backspace"
Layout.preferredWidth: 250 Layout.preferredWidth: 200
onClicked: { onClicked: {
UtilitiesTabController.sendKeyboardBackspace(1) UtilitiesTabController.sendKeyboardBackspace(1)
} }
@@ -77,7 +85,7 @@ MyStackViewPage {
MyPushButton { MyPushButton {
text: "10x Backspace" text: "10x Backspace"
Layout.preferredWidth: 250 Layout.preferredWidth: 200
onClicked: { onClicked: {
UtilitiesTabController.sendKeyboardBackspace(10) UtilitiesTabController.sendKeyboardBackspace(10)
} }
+1
View File
@@ -16,6 +16,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(int count) = 0;
virtual void sendKeyboardAltTab() = 0;
}; };
} }
@@ -61,6 +61,10 @@ namespace advsettings {
keyboardInput->sendKeyboardEnter(); keyboardInput->sendKeyboardEnter();
} }
void UtilitiesTabController::sendKeyboardAltTab() {
keyboardInput->sendKeyboardAltTab();
}
void UtilitiesTabController::sendKeyboardBackspace(int count) { void UtilitiesTabController::sendKeyboardBackspace(int count) {
keyboardInput->sendKeyboardBackspace(count); keyboardInput->sendKeyboardBackspace(count);
} }
@@ -59,6 +59,7 @@ namespace advsettings {
public slots: public slots:
void sendKeyboardInput(QString input); void sendKeyboardInput(QString input);
void sendKeyboardEnter(); void sendKeyboardEnter();
void sendKeyboardAltTab();
void sendKeyboardBackspace(int count); void sendKeyboardBackspace(int count);
void setAlarmEnabled(bool enabled, bool notify = true); void setAlarmEnabled(bool enabled, bool notify = true);
@@ -17,4 +17,9 @@ void KeyboardInputDummy::sendKeyboardBackspace(int count)
// noop // noop
} }
void KeyboardInputDummy::sendKeyboardAltTab()
{
// noop
}
} }
@@ -12,6 +12,7 @@ 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(int count) override;
virtual void sendKeyboardAltTab() override;
}; };
} }
@@ -17,6 +17,24 @@ void fillKiStruct(INPUT& ip, WORD scanCode, bool keyup) {
ip.ki.time = 0; ip.ki.time = 0;
}; };
void sendKeyboardInputRaw(int inputCount, LPINPUT input) {
if (!SendInput(inputCount, input, sizeof(INPUT))) {
char *err;
auto errCode = GetLastError();
if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
(LPTSTR)&err,
0,
NULL)){
LOG(ERROR) << "Error calling SendInput(): Could not get error message (" << errCode << ")";
} else {
LOG(ERROR) << "Error calling SendInput(): " << err;
}
}
}
void KeyboardInputWindows::sendKeyboardInput(QString input) void KeyboardInputWindows::sendKeyboardInput(QString input)
{ {
int len = input.length(); int len = input.length();
@@ -69,21 +87,8 @@ void KeyboardInputWindows::sendKeyboardInput(QString input)
fillKiStruct(ips[ii++], VK_MENU, true); fillKiStruct(ips[ii++], VK_MENU, true);
altPressed = false; altPressed = false;
} }
if (!SendInput(ii, ips, sizeof(INPUT))) {
char *err; sendKeyboardInputRaw(ii, ips);
auto errCode = GetLastError();
if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
(LPTSTR)&err,
0,
NULL)){
LOG(ERROR) << "Error calling SendInput(): Could not get error message (" << errCode << ")";
} else {
LOG(ERROR) << "Error calling SendInput(): " << err;
}
}
delete ips; delete ips;
} }
} }
@@ -93,21 +98,8 @@ void KeyboardInputWindows::sendKeyboardEnter()
LPINPUT ips = new INPUT[2]; LPINPUT ips = new INPUT[2];
fillKiStruct(ips[0], VK_RETURN, false); fillKiStruct(ips[0], VK_RETURN, false);
fillKiStruct(ips[1], VK_RETURN, true); fillKiStruct(ips[1], VK_RETURN, true);
if (!SendInput(2, ips, sizeof(INPUT))) {
char *err; sendKeyboardInputRaw(2, ips);
auto errCode = GetLastError();
if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
(LPTSTR)&err,
0,
NULL)) {
LOG(ERROR) << "Error calling SendInput(): Could not get error message (" << errCode << ")";
} else {
LOG(ERROR) << "Error calling SendInput(): " << err;
}
}
delete ips; delete ips;
} }
@@ -119,23 +111,23 @@ void KeyboardInputWindows::sendKeyboardBackspace(int count)
fillKiStruct(ips[2 * i], VK_BACK, false); fillKiStruct(ips[2 * i], VK_BACK, false);
fillKiStruct(ips[2 * i + 1], VK_BACK, true); fillKiStruct(ips[2 * i + 1], VK_BACK, true);
} }
if (!SendInput(2 * count, ips, sizeof(INPUT))) {
char *err; sendKeyboardInputRaw(count * 2, ips);
auto errCode = GetLastError();
if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
(LPTSTR)&err,
0,
NULL)) {
LOG(ERROR) << "Error calling SendInput(): Could not get error message (" << errCode << ")";
} else {
LOG(ERROR) << "Error calling SendInput(): " << err;
}
}
delete ips; delete ips;
} }
} }
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);
sendKeyboardInputRaw(4, ips);
delete ips;
}
} }
@@ -12,6 +12,7 @@ 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(int count) override;
virtual void sendKeyboardAltTab() override;
}; };
} }