diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..da33227 --- /dev/null +++ b/.clang-format @@ -0,0 +1,12 @@ +BasedOnStyle: Google +IndentWidth: 2 + +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortEnumsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: Empty +AllowShortLoopsOnASingleLine: false +DerivePointerAlignment: false +PointerAlignment: Left diff --git a/index.js b/index.js index 6bd5ca0..80afda1 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,16 @@ const path = require("path"); const shortcut = require("windows-shortcuts"); const lib = require("bindings")("serenade-driver.node"); +const defaultKeyDelay = () => { + if (os.platform() == "darwin") { + return 8; + } else if (os.platform() == "win32") { + return 2; + } + + return 3; +}; + const applicationMatches = (application, possible, aliases) => { let alias = application; if (aliases && aliases[application]) { @@ -105,10 +115,6 @@ exports.getEditorState = () => { return lib.getEditorState(); }; -exports.getEditorStateFallback = (paragraph) => { - return lib.getEditorStateFallback(!!paragraph); -}; - exports.getInstalledApplications = async () => { const search = async (root, depth, max) => { let result = []; @@ -150,8 +156,7 @@ exports.getInstalledApplications = async () => { max ) ) - .concat(await search("C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs", 0, max) - ); + .concat(await search("C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs", 0, max)); } return Promise.resolve([]); @@ -226,11 +231,15 @@ exports.mouseUp = (button) => { return lib.mouseUp(button); }; -exports.pressKey = (key, modifiers, count) => { +exports.pressKey = (key, modifiers, count, delay) => { if (!modifiers) { modifiers = []; } + if (delay === undefined) { + delay = defaultKeyDelay(); + } + if (count === undefined || count === false) { count = 1; } @@ -239,7 +248,7 @@ exports.pressKey = (key, modifiers, count) => { return; } - return lib.pressKey(key, modifiers, count); + return lib.pressKey(key, modifiers, count, delay); }; exports.quitApplication = async (application, aliases) => { @@ -301,10 +310,14 @@ exports.setMouseLocation = (x, y) => { return lib.setMouseLocation(x, y); }; -exports.typeText = (text) => { +exports.typeText = (text, delay) => { if (!text) { return; } - return lib.typeText(text); + if (delay === undefined) { + delay = defaultKeyDelay(); + } + + return lib.typeText(text, delay); }; diff --git a/src/driver.cpp b/src/driver.cpp index 8117fd3..3669d57 100644 --- a/src/driver.cpp +++ b/src/driver.cpp @@ -155,30 +155,6 @@ Napi::Promise GetEditorState(const Napi::CallbackInfo& info) { return deferred.Promise(); } -Napi::Promise GetEditorStateFallback(const Napi::CallbackInfo& info) { - Napi::Env env = info.Env(); - Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env); - - bool paragraph = info[0].As().Value(); - -#ifdef __linux__ - Display* display = XOpenDisplay(NULL); - std::tuple state = driver::GetEditorStateFallback(display, paragraph); - XCloseDisplay(display); -#else - std::tuple state; - AUTORELEASE(state = driver::GetEditorStateFallback(paragraph)); -#endif - - Napi::Object result = Napi::Object::New(env); - result.Set("text", std::get<0>(state)); - result.Set("cursor", std::get<1>(state)); - result.Set("error", std::get<2>(state)); - deferred.Resolve(result); - - return deferred.Promise(); -} - Napi::Promise GetMouseLocation(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env); @@ -247,6 +223,8 @@ Napi::Promise PressKey(const Napi::CallbackInfo& info) { Napi::Array modifierArray = info[1].As(); int count = info[2].As().Int32Value(); + int delay = info[3].As().Int32Value(); + if (count < 1) { deferred.Resolve(env.Undefined()); return deferred.Promise(); @@ -264,9 +242,9 @@ Napi::Promise PressKey(const Napi::CallbackInfo& info) { for (int i = 0; i < count; i++) { #ifdef __linux__ - driver::PressKey(display, info[0].As().Utf8Value(), modifiers); + driver::PressKey(display, info[0].As().Utf8Value(), modifiers, delay); #else - AUTORELEASE(driver::PressKey(info[0].As().Utf8Value(), modifiers)); + AUTORELEASE(driver::PressKey(info[0].As().Utf8Value(), modifiers, delay)); #endif } @@ -309,6 +287,7 @@ Napi::Promise TypeText(const Napi::CallbackInfo& info) { std::vector modifiers; std::string text = info[0].As().Utf8Value(); + int delay = info[1].As().Int32Value(); #ifdef __linux__ Display* display = XOpenDisplay(NULL); @@ -316,9 +295,9 @@ Napi::Promise TypeText(const Napi::CallbackInfo& info) { for (char c : text) { #ifdef __linux__ - driver::PressKey(display, std::string(1, c), modifiers); + driver::PressKey(display, std::string(1, c), modifiers, delay); #else - AUTORELEASE(driver::PressKey(std::string(1, c), modifiers)); + AUTORELEASE(driver::PressKey(std::string(1, c), modifiers, delay)); #endif } @@ -342,8 +321,6 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) { exports.Set(Napi::String::New(env, "getClickableButtons"), Napi::Function::New(env, GetClickableButtons)); exports.Set(Napi::String::New(env, "getEditorState"), Napi::Function::New(env, GetEditorState)); - exports.Set(Napi::String::New(env, "getEditorStateFallback"), - Napi::Function::New(env, GetEditorStateFallback)); exports.Set(Napi::String::New(env, "getMouseLocation"), Napi::Function::New(env, GetMouseLocation)); exports.Set(Napi::String::New(env, "getRunningApplications"), diff --git a/src/driver.hpp b/src/driver.hpp index eef0897..98437ca 100644 --- a/src/driver.hpp +++ b/src/driver.hpp @@ -9,7 +9,6 @@ Napi::Promise GetActiveApplication(const Napi::CallbackInfo& info); Napi::Promise GetActiveApplicationWindowBounds(const Napi::CallbackInfo& info); Napi::Promise GetClickableButtons(const Napi::CallbackInfo& info); Napi::Promise GetEditorState(const Napi::CallbackInfo& info); -Napi::Promise GetEditorStateFallback(const Napi::CallbackInfo& info); Napi::Promise GetMouseLocation(const Napi::CallbackInfo& info); Napi::Promise GetRunningApplications(const Napi::CallbackInfo& info); Napi::Promise MouseDown(const Napi::CallbackInfo& info); diff --git a/src/linux.cpp b/src/linux.cpp index 7f406f5..fbdc383 100644 --- a/src/linux.cpp +++ b/src/linux.cpp @@ -176,34 +176,6 @@ std::tuple GetEditorState(Display* display) { return result; } -std::tuple GetEditorStateFallback(Display* display, - bool paragraph) { - std::tuple result; - - unsigned long color = BlackPixel(display, DefaultScreen(display)); - Window window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, - 1, 1, 0, color, color); - - PressKey(display, paragraph ? "up" : "home", - std::vector{"control", "shift"}); - PressKey(display, "c", std::vector{"control"}); - usleep(10000); - PressKey(display, "right", std::vector{}); - std::string left = GetClipboard(display, window); - - PressKey(display, paragraph ? "down" : "end", - std::vector{"control", "shift"}); - PressKey(display, "c", std::vector{"control"}); - usleep(10000); - PressKey(display, "left", std::vector{}); - std::string right = GetClipboard(display, window); - - std::get<0>(result) = left + right; - std::get<1>(result) = left.length(); - std::get<2>(result) = false; - return result; -} - std::tuple GetKeycodeAndModifiers(Display* display, const std::string& key) { std::tuple result; @@ -453,7 +425,7 @@ void MouseUp(Display* display, const std::string& button) { } void PressKey(Display* display, std::string key, - std::vector modifiers) { + std::vector modifiers, int delay) { std::tuple keycodeAndModifiers = GetKeycodeAndModifiers(display, key); int keycode = std::get<0>(keycodeAndModifiers); @@ -489,7 +461,7 @@ void PressKey(Display* display, std::string key, ToggleKey(display, "shift", false); } - usleep(3000); + usleep(delay * 1000); } std::string ProcessName(Display* display, Window window) { diff --git a/src/linux.hpp b/src/linux.hpp index 74e17ee..dc25fc8 100644 --- a/src/linux.hpp +++ b/src/linux.hpp @@ -14,8 +14,6 @@ std::tuple GetActiveApplicationWindowBounds(); std::vector GetAllWindows(Display* display); std::string GetClipboard(Display* display, Window window); std::tuple GetEditorState(Display* display); -std::tuple GetEditorStateFallback(Display* display, - bool paragraph); std::tuple GetKeycodeAndModifiers(Display* display, const std::string& key); std::tuple GetMouseLocation(); @@ -25,7 +23,7 @@ std::vector GetRunningApplications(); void MouseDown(Display* display, const std::string& button); void MouseUp(Display* display, const std::string& button); void PressKey(Display* display, std::string key, - std::vector modifiers); + std::vector modifiers, int delay); std::string ProcessName(Display* display, Window window); void SetMouseLocation(int x, int y); void ToggleKey(Display* display, const std::string& key, bool down); diff --git a/src/mac.cpp b/src/mac.cpp index eaa9f08..bff1e65 100644 --- a/src/mac.cpp +++ b/src/mac.cpp @@ -10,6 +10,7 @@ #include #include "mac.hpp" +#include "util.hpp" namespace driver { @@ -273,7 +274,8 @@ std::vector GetVisiblePids() { NSRunningApplication* app = [NSRunningApplication runningApplicationWithProcessIdentifier:pid]; if (app != NULL && app.bundleURL != NULL) { NSString* name = app.bundleURL.path.lowercaseString; - // filter out applications that appear to have windows, but are actually system services + // filter out applications that appear to have windows, but are actually + // system services if (![name hasSuffix:@".xpc"] && ![name hasSuffix:@".appex"] && ![name containsString:@"coreservices"] && ![name containsString:@"privateframeworks"] && ![name containsString:@".framework"] && ![name containsString:@"helper"] && @@ -471,11 +473,15 @@ std::tuple GetEditorState() { [sourceData length] / sizeof(wchar_t)); std::string narrow(source.begin(), source.end()); - // Bulleted lists in the Slack app send these characters in place of the bullet - // 3n + 1-th level: u\0006 (acknowledge) - // 3n + 2-th level: u\0007 (bell) - // 3n-th level: \t (tab) - // We replace the first two with a space to maintain the proper cursor position + // Bulleted lists in the Slack app send these characters in place of the + // bullet: + // + // * 3n + 1-th level: u\0006 (acknowledge) + // * 3n + 2-th level: u\0007 (bell) + // * 3n-th level: \t (tab) + // + // We replace the first two with a space to + // maintain the proper cursor position for (CFIndex i = 0; i < (int)narrow.size(); i++) { if (narrow[i] == '\u0006' || narrow[i] == '\u0007') { narrow[i] = ' '; @@ -489,8 +495,8 @@ std::tuple GetEditorState() { int newLineCount = 0; for (CFIndex i = 0; (i < range.location + newLineCount) && (i < (int)narrow.size()); i++) { if (narrow[i] == '\n') { - // Double newlines update the range.location property correctly, so avoid double - // counting + // Double newlines update the range.location property correctly, so + // avoid double counting if (i > 0 && narrow[i - 1] != '\n') { newLineCount++; } @@ -514,64 +520,6 @@ std::tuple GetEditorState() { return result; } -std::tuple GetEditorStateFallback(bool paragraph) { - long delay = 30000; - std::tuple result; - std::get<2>(result) = true; - - NSPasteboard* pasteboard = NSPasteboard.generalPasteboard; - [pasteboard declareTypes:@[ NSPasteboardTypeString ] owner:NULL]; - NSString* previous = @""; - if (pasteboard.pasteboardItems.count > 0) { - previous = [pasteboard.pasteboardItems[0] stringForType:NSPasteboardTypeString]; - } - - if (paragraph) { - PressKey("up", std::vector{"option", "shift"}); - usleep(delay); - } else { - PressKey("left", std::vector{"command", "shift"}); - usleep(delay); - PressKey("up", std::vector{"command", "shift"}); - usleep(delay); - } - - PressKey("c", std::vector{"command"}); - usleep(delay); - PressKey("right", std::vector{}); - usleep(delay); - - if (pasteboard.pasteboardItems.count == 0) { - return result; - } - NSString* left = [pasteboard.pasteboardItems[0] stringForType:NSPasteboardTypeString]; - - if (paragraph) { - PressKey("down", std::vector{"option", "shift"}); - usleep(delay); - } else { - PressKey("right", std::vector{"command", "shift"}); - usleep(delay); - PressKey("down", std::vector{"command", "shift"}); - usleep(delay); - } - - PressKey("c", std::vector{"command"}); - usleep(delay); - PressKey("left", std::vector{}); - - if (pasteboard.pasteboardItems.count == 0) { - return result; - } - NSString* right = [pasteboard.pasteboardItems[0] stringForType:NSPasteboardTypeString]; - - [pasteboard setString:previous forType:NSPasteboardTypeString]; - std::get<0>(result) = [[NSString stringWithFormat:@"%@%@", left, right] UTF8String]; - std::get<1>(result) = left.length; - std::get<2>(result) = false; - return result; -} - std::tuple GetMouseLocation() { std::tuple result; std::get<0>(result) = NSEvent.mouseLocation.x; @@ -674,69 +622,71 @@ std::tuple GetVirtualKeyAndModifiers(const std::string& k return result; } - if (key == "enter" || key == "\n" || key == "return") { + std::string lower = key; + ToLower(lower); + if (lower == "enter" || key == "\n" || lower == "return") { std::get<0>(result) = CGKeyCode(kVK_Return); - } else if (key == "tab" || key == "\t") { + } else if (lower == "tab" || key == "\t") { std::get<0>(result) = CGKeyCode(kVK_Tab); - } else if (key == "space" || key == " ") { + } else if (lower == "space" || key == " ") { std::get<0>(result) = CGKeyCode(kVK_Space); - } else if (key == "backspace" || key == "delete") { + } else if (lower == "backspace" || lower == "delete") { std::get<0>(result) = CGKeyCode(kVK_Delete); - } else if (key == "forwarddelete") { + } else if (lower == "forwarddelete") { std::get<0>(result) = CGKeyCode(kVK_ForwardDelete); - } else if (key == "escape") { + } else if (lower == "escape") { std::get<0>(result) = CGKeyCode(kVK_Escape); - } else if (key == "command" || key == "cmd" || key == "commandOrControl") { + } else if (lower == "command" || lower == "cmd" || lower == "commandOrControl") { std::get<0>(result) = CGKeyCode(kVK_Command); - } else if (key == "caps") { + } else if (lower == "caps") { std::get<0>(result) = CGKeyCode(kVK_CapsLock); - } else if (key == "shift") { + } else if (lower == "shift") { std::get<0>(result) = CGKeyCode(kVK_Shift); - } else if (key == "option" || key == "alt") { + } else if (lower == "option" || lower == "alt") { std::get<0>(result) = CGKeyCode(kVK_Option); - } else if (key == "control" || key == "ctrl") { + } else if (lower == "control" || lower == "ctrl") { std::get<0>(result) = CGKeyCode(kVK_Control); - } else if (key == "function" || key == "fn") { + } else if (lower == "function" || lower == "fn") { std::get<0>(result) = CGKeyCode(kVK_Function); - } else if (key == "home") { + } else if (lower == "home") { std::get<0>(result) = CGKeyCode(kVK_Home); - } else if (key == "pageup") { + } else if (lower == "pageup") { std::get<0>(result) = CGKeyCode(kVK_PageUp); - } else if (key == "end") { + } else if (lower == "end") { std::get<0>(result) = CGKeyCode(kVK_End); - } else if (key == "pagedown") { + } else if (lower == "pagedown") { std::get<0>(result) = CGKeyCode(kVK_PageDown); - } else if (key == "left") { + } else if (lower == "left") { std::get<0>(result) = CGKeyCode(kVK_LeftArrow); - } else if (key == "right") { + } else if (lower == "right") { std::get<0>(result) = CGKeyCode(kVK_RightArrow); - } else if (key == "down") { + } else if (lower == "down") { std::get<0>(result) = CGKeyCode(kVK_DownArrow); - } else if (key == "up") { + } else if (lower == "up") { std::get<0>(result) = CGKeyCode(kVK_UpArrow); - } else if (key == "f1") { + } else if (lower == "f1") { std::get<0>(result) = CGKeyCode(kVK_F1); - } else if (key == "f2") { + } else if (lower == "f2") { std::get<0>(result) = CGKeyCode(kVK_F2); - } else if (key == "f3") { + } else if (lower == "f3") { std::get<0>(result) = CGKeyCode(kVK_F3); - } else if (key == "f4") { + } else if (lower == "f4") { std::get<0>(result) = CGKeyCode(kVK_F4); - } else if (key == "f5") { + } else if (lower == "f5") { std::get<0>(result) = CGKeyCode(kVK_F5); - } else if (key == "f6") { + } else if (lower == "f6") { std::get<0>(result) = CGKeyCode(kVK_F6); - } else if (key == "f7") { + } else if (lower == "f7") { std::get<0>(result) = CGKeyCode(kVK_F7); - } else if (key == "f8") { + } else if (lower == "f8") { std::get<0>(result) = CGKeyCode(kVK_F8); - } else if (key == "f9") { + } else if (lower == "f9") { std::get<0>(result) = CGKeyCode(kVK_F9); - } else if (key == "f10") { + } else if (lower == "f10") { std::get<0>(result) = CGKeyCode(kVK_F10); - } else if (key == "f11") { + } else if (lower == "f11") { std::get<0>(result) = CGKeyCode(kVK_F11); - } else if (key == "f12") { + } else if (lower == "f12") { std::get<0>(result) = CGKeyCode(kVK_F12); } @@ -829,9 +779,9 @@ void MouseUp(const std::string& button) { CFRelease(event); } -void PressKey(const std::string& key, const std::vector& modifiers) { - ToggleKey(key, modifiers, true); - ToggleKey(key, modifiers, false); +void PressKey(const std::string& key, const std::vector& modifiers, int delay) { + ToggleKey(key, modifiers, true, delay); + ToggleKey(key, modifiers, false, delay); } void SetEditorState(const std::string& text, int cursor, int cursorEnd) { @@ -876,7 +826,8 @@ void SetMouseLocation(int x, int y) { usleep(100000); } -void ToggleKey(const std::string& key, const std::vector& modifiers, bool down) { +void ToggleKey(const std::string& key, const std::vector& modifiers, bool down, + int delay) { std::vector adjustedModifiers = modifiers; std::tuple keycode = GetVirtualKeyAndModifiers(key); if (std::get<1>(keycode)) { @@ -943,12 +894,7 @@ void ToggleKey(const std::string& key, const std::vector& modifiers CFRelease(source); CFRelease(event); - // determined empirically by typing into a variety of applications - usleep(8000); -} - -void ToLower(std::string& s) { - std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); }); + usleep(delay * 1000); } } // namespace driver diff --git a/src/mac.hpp b/src/mac.hpp index 8bc6c65..fdd8167 100644 --- a/src/mac.hpp +++ b/src/mac.hpp @@ -30,7 +30,6 @@ void GetClickableButtons(AXUIElementRef element, std::vector& result); std::vector GetClickableButtons(); std::tuple GetEditorState(); -std::tuple GetEditorStateFallback(bool paragraph); std::tuple GetMouseLocation(); CFStringRef GetLines(AXUIElementRef element); CFStringRef GetLineText(AXUIElementRef element, CFMutableArrayRef textChildren); @@ -44,12 +43,12 @@ bool HasActionName(AXUIElementRef element, CFStringRef name); bool IsButton(AXUIElementRef element); void MouseDown(const std::string& button); void MouseUp(const std::string& button); -void PressKey(const std::string& key, - const std::vector& modifiers); +void PressKey(const std::string& key, const std::vector& modifiers, + int delay); void SetEditorState(const std::string& text, int cursor, int cursorEnd); void SetMouseLocation(int x, int y); void ToggleKey(const std::string& key, - const std::vector& modifiers, bool down); + const std::vector& modifiers, bool down, int delay); void ToLower(std::string& s); } // namespace driver diff --git a/src/util.hpp b/src/util.hpp index 391a69a..3a694a3 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -2,11 +2,13 @@ #include namespace driver { - void RemoveSpaces(std::string& s) { - s.erase(std::remove_if(s.begin(), s.end(), isspace), s.end()); - } +void RemoveSpaces(std::string& s) { + s.erase(std::remove_if(s.begin(), s.end(), isspace), s.end()); +} - void ToLower(std::string& s) { - std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); }); - } -} \ No newline at end of file +void ToLower(std::string& s) { + std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { + return std::tolower(c); + }); +} +} // namespace driver diff --git a/src/windows.cpp b/src/windows.cpp index 44059d3..8591ba8 100644 --- a/src/windows.cpp +++ b/src/windows.cpp @@ -13,8 +13,8 @@ #include #include -#include "windows.hpp" #include "util.hpp" +#include "windows.hpp" namespace driver { @@ -180,56 +180,6 @@ std::tuple GetEditorState() { return result; } -std::tuple GetEditorStateFallback(bool paragraph) { - std::tuple result; - - DWORD delay = 50; - if (paragraph) { - PressKey("home", std::vector{"shift"}); - Sleep(delay); - PressKey("up", std::vector{"control", "shift"}); - } else { - PressKey("home", std::vector{"control", "shift"}); - } - - Sleep(delay); - PressKey("c", std::vector{"control"}); - Sleep(delay); - PressKey("right", std::vector{}); - std::string left = GetClipboard(); - RemoveNonASCII(left); - - // when copying left, the preceding newline is included - if (left.length() > 0 && left[0] == '\n') { - left = left.substr(1); - } - - if (paragraph) { - PressKey("end", std::vector{"shift"}); - Sleep(delay); - PressKey("down", std::vector{"control", "shift"}); - } else { - PressKey("end", std::vector{"control", "shift"}); - } - - Sleep(delay); - PressKey("c", std::vector{"control"}); - Sleep(delay); - PressKey("left", std::vector{}); - std::string right = GetClipboard(); - RemoveNonASCII(right); - - // when copying right, an extra space is included - if (right.length() > 0 && right[right.length() - 1] == ' ') { - right = right.substr(0, right.length() - 1); - } - - std::get<0>(result) = left + right; - std::get<1>(result) = left.length(); - std::get<2>(result) = false; - return result; -} - std::tuple GetMouseLocation() { POINT point; GetCursorPos(&point); @@ -270,69 +220,72 @@ std::tuple GetVirtualKeyAndModifiers( std::tuple result; std::get<0>(result) = -1; - if (key == "left") { + std::string lower = key; + ToLower(lower); + if (lower == "left") { std::get<0>(result) = VK_LEFT; - } else if (key == "right") { + } else if (lower == "right") { std::get<0>(result) = VK_RIGHT; - } else if (key == "up") { + } else if (lower == "up") { std::get<0>(result) = VK_UP; - } else if (key == "down") { + } else if (lower == "down") { std::get<0>(result) = VK_DOWN; - } else if (key == "control" || key == "ctrl" || key == "commandOrControl") { + } else if (lower == "control" || lower == "ctrl" || + lower == "commandOrControl") { std::get<0>(result) = VK_CONTROL; - } else if (key == "alt" || key == "option") { + } else if (lower == "alt" || lower == "option") { std::get<0>(result) = VK_MENU; - } else if (key == "shift") { + } else if (lower == "shift") { std::get<0>(result) = VK_SHIFT; - } else if (key == "backspace") { + } else if (lower == "backspace") { std::get<0>(result) = VK_BACK; - } else if (key == "delete") { + } else if (lower == "delete") { std::get<0>(result) = VK_DELETE; - } else if (key == "tab" || key == "\t") { + } else if (lower == "tab" || key == "\t") { std::get<0>(result) = VK_TAB; - } else if (key == "space" || key == " ") { + } else if (lower == "space" || key == " ") { std::get<0>(result) = VK_SPACE; - } else if (key == "caps") { + } else if (lower == "caps") { std::get<0>(result) = VK_CAPITAL; - } else if (key == "meta" || key == "win" || key == "windows") { + } else if (lower == "meta" || lower == "win" || lower == "windows") { std::get<0>(result) = VK_LWIN; - } else if (key == "escape") { + } else if (lower == "escape") { std::get<0>(result) = VK_ESCAPE; - } else if (key == "enter" || key == "return" || key == "\n") { + } else if (lower == "enter" || lower == "return" || key == "\n") { std::get<0>(result) = VK_RETURN; - } else if (key == "pageup") { + } else if (lower == "pageup") { std::get<0>(result) = VK_PRIOR; - } else if (key == "pagedown") { + } else if (lower == "pagedown") { std::get<0>(result) = VK_NEXT; - } else if (key == "home") { + } else if (lower == "home") { std::get<0>(result) = VK_HOME; std::get<3>(result) = 1; - } else if (key == "end") { + } else if (lower == "end") { std::get<0>(result) = VK_END; std::get<3>(result) = 1; - } else if (key == "f1") { + } else if (lower == "f1") { std::get<0>(result) = VK_F1; - } else if (key == "f2") { + } else if (lower == "f2") { std::get<0>(result) = VK_F2; - } else if (key == "f3") { + } else if (lower == "f3") { std::get<0>(result) = VK_F3; - } else if (key == "f4") { + } else if (lower == "f4") { std::get<0>(result) = VK_F4; - } else if (key == "f5") { + } else if (lower == "f5") { std::get<0>(result) = VK_F5; - } else if (key == "f6") { + } else if (lower == "f6") { std::get<0>(result) = VK_F6; - } else if (key == "f7") { + } else if (lower == "f7") { std::get<0>(result) = VK_F7; - } else if (key == "f8") { + } else if (lower == "f8") { std::get<0>(result) = VK_F8; - } else if (key == "f9") { + } else if (lower == "f9") { std::get<0>(result) = VK_F9; - } else if (key == "f10") { + } else if (lower == "f10") { std::get<0>(result) = VK_F10; - } else if (key == "f11") { + } else if (lower == "f11") { std::get<0>(result) = VK_F11; - } else if (key == "f12") { + } else if (lower == "f12") { std::get<0>(result) = VK_F12; } @@ -381,7 +334,8 @@ void MouseUp(const std::string& button) { } } -void PressKey(const std::string& key, std::vector modifiers) { +void PressKey(const std::string& key, std::vector modifiers, + int delay) { for (std::string modifier : modifiers) { ToggleKey(modifier, true); } @@ -393,7 +347,7 @@ void PressKey(const std::string& key, std::vector modifiers) { ToggleKey(modifier, false); } - Sleep(2); + Sleep(delay); } std::string ProcessName(HWND window) { @@ -419,7 +373,9 @@ void RemoveNonASCII(std::string& s) { } } -void SetMouseLocation(int x, int y) { SetCursorPos(x, y); } +void SetMouseLocation(int x, int y) { + SetCursorPos(x, y); +} void ToggleKey(const std::string& key, bool down) { // first, look for a hard-coded virtual key (e.g., for non-alphanumeric diff --git a/src/windows.hpp b/src/windows.hpp index 94cf8aa..3a120ca 100644 --- a/src/windows.hpp +++ b/src/windows.hpp @@ -13,7 +13,6 @@ std::string GetActiveApplication(); std::tuple GetActiveApplicationWindowBounds(); std::string GetClipboard(); std::tuple GetEditorState(); -std::tuple GetEditorStateFallback(bool paragraph); std::tuple GetMouseLocation(); std::vector GetRunningApplications(); BOOL CALLBACK GetRunningWindows(HWND window, LPARAM data); @@ -22,7 +21,8 @@ std::tuple GetVirtualKeyAndModifiers( void InitializeUIAutomation(); void MouseDown(const std::string& button); void MouseUp(const std::string& button); -void PressKey(const std::string& key, std::vector modifiers); +void PressKey(const std::string& key, std::vector modifiers, + int delay); std::string ProcessName(HWND window); void RemoveNonASCII(std::string& s); void SetMouseLocation(int x, int y);