Add option to send Alt-Tab as keyboard input

This commit is contained in:
Robin Appelman
2018-12-26 16:32:38 +01:00
parent 835c51e937
commit 8e2ca3602a
8 changed files with 62 additions and 49 deletions
+12 -4
View File
@@ -45,7 +45,7 @@ MyStackViewPage {
MyPushButton {
text: "Keyboard Input"
Layout.preferredWidth: 250
Layout.preferredWidth: 200
onClicked: {
OverlayController.showKeyboard("", 601)
}
@@ -61,15 +61,23 @@ MyStackViewPage {
MyPushButton {
text: "Enter"
Layout.preferredWidth: 250
Layout.preferredWidth: 200
onClicked: {
UtilitiesTabController.sendKeyboardEnter()
}
}
MyPushButton {
text: "Alt-Tab"
Layout.preferredWidth: 200
onClicked: {
UtilitiesTabController.sendKeyboardAltTab()
}
}
MyPushButton {
text: "Backspace"
Layout.preferredWidth: 250
Layout.preferredWidth: 200
onClicked: {
UtilitiesTabController.sendKeyboardBackspace(1)
}
@@ -77,7 +85,7 @@ MyStackViewPage {
MyPushButton {
text: "10x Backspace"
Layout.preferredWidth: 250
Layout.preferredWidth: 200
onClicked: {
UtilitiesTabController.sendKeyboardBackspace(10)
}
+1
View File
@@ -16,6 +16,7 @@ public:
virtual void sendKeyboardInput(QString input) = 0;
virtual void sendKeyboardEnter() = 0;
virtual void sendKeyboardBackspace(int count) = 0;
virtual void sendKeyboardAltTab() = 0;
};
}
@@ -61,6 +61,10 @@ namespace advsettings {
keyboardInput->sendKeyboardEnter();
}
void UtilitiesTabController::sendKeyboardAltTab() {
keyboardInput->sendKeyboardAltTab();
}
void UtilitiesTabController::sendKeyboardBackspace(int count) {
keyboardInput->sendKeyboardBackspace(count);
}
@@ -59,6 +59,7 @@ namespace advsettings {
public slots:
void sendKeyboardInput(QString input);
void sendKeyboardEnter();
void sendKeyboardAltTab();
void sendKeyboardBackspace(int count);
void setAlarmEnabled(bool enabled, bool notify = true);
@@ -17,4 +17,9 @@ void KeyboardInputDummy::sendKeyboardBackspace(int count)
// noop
}
void KeyboardInputDummy::sendKeyboardAltTab()
{
// noop
}
}
@@ -12,6 +12,7 @@ public:
virtual void sendKeyboardInput(QString input) override;
virtual void sendKeyboardEnter() 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;
};
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)
{
int len = input.length();
@@ -69,21 +87,8 @@ void KeyboardInputWindows::sendKeyboardInput(QString input)
fillKiStruct(ips[ii++], VK_MENU, true);
altPressed = false;
}
if (!SendInput(ii, ips, 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;
}
}
sendKeyboardInputRaw(ii, ips);
delete ips;
}
}
@@ -93,21 +98,8 @@ void KeyboardInputWindows::sendKeyboardEnter()
LPINPUT ips = new INPUT[2];
fillKiStruct(ips[0], VK_RETURN, false);
fillKiStruct(ips[1], VK_RETURN, true);
if (!SendInput(2, ips, 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;
}
}
sendKeyboardInputRaw(2, ips);
delete ips;
}
@@ -119,23 +111,23 @@ void KeyboardInputWindows::sendKeyboardBackspace(int count)
fillKiStruct(ips[2 * i], VK_BACK, false);
fillKiStruct(ips[2 * i + 1], VK_BACK, true);
}
if (!SendInput(2 * count, ips, 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;
}
}
sendKeyboardInputRaw(count * 2, 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 sendKeyboardEnter() override;
virtual void sendKeyboardBackspace(int count) override;
virtual void sendKeyboardAltTab() override;
};
}