Skip to content

Instantly share code, notes, and snippets.

@janoamaral
Created September 27, 2020 15:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janoamaral/060cf89f5e5f7cb30798a3338e917796 to your computer and use it in GitHub Desktop.
Save janoamaral/060cf89f5e5f7cb30798a3338e917796 to your computer and use it in GitHub Desktop.
Add web search capabilities to `swhd`. Usage: Select text in any program and press `WIN+ALT+S`. A new Chrome tab comes up with Duckduck search result.
diff --git a/config.def.h b/config.def.h
index 1dbc8ca..b01d887 100644
--- a/config.def.h
+++ b/config.def.h
@@ -15,6 +15,7 @@ const int VK_C = 0x43;
// CUSTOM FUNCTION ID
const unsigned int FUNCTION_RELOAD = 1;
const unsigned int FUNCTION_RELOAD = 2;
+const unsigned int FUNCTION_SEARCH = 3;
// HOTKEY DECLARATION
@@ -22,5 +23,6 @@ static const Key keys[] = {
/* modifier/s key internal function program path */
{MOD_WIN, VK_F2, NULL, "C:\\Windows\\notepad.exe" },
{MOD_WIN, VK_F12, FUNCTION_RELOAD, NULL },
- {MOD_WIN + MOD_ALT, VK_C, NULL, "C:\\Windows\\write.exe", }
+ {MOD_WIN + MOD_ALT, VK_C, NULL, "C:\\Windows\\write.exe", },
+ {MOD_WIN + MOD_ALT, VK_S, FUNCTION_SEARCH, NULL }
};
diff --git a/custfunc.c b/custfunc.c
index 7cf2aff..aaaf0de 100644
--- a/custfunc.c
+++ b/custfunc.c
@@ -35,3 +35,60 @@ void RunCommand(char* command) {
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
+
+void SearchSelected()
+{
+ // This structure will be used to create the keyboard
+ // input event.
+ INPUT ip;
+ ip.type = INPUT_KEYBOARD;
+ ip.ki.wScan = 0;
+ ip.ki.time = 0;
+ ip.ki.dwExtraInfo = 0;
+
+ // Pause for 250 milliseconds.
+ Sleep(200);
+
+
+ // Press the "Ctrl" key
+ ip.ki.wVk = VK_CONTROL;
+ ip.ki.dwFlags = 0; // 0 for key press
+ SendInput(1, &ip, sizeof(INPUT));
+
+ // Press the "C" key
+ ip.ki.wVk = 'C';
+ ip.ki.dwFlags = 0; // 0 for key press
+ SendInput(1, &ip, sizeof(INPUT));
+
+ // Release the "C" key
+ ip.ki.wVk = 'C';
+ ip.ki.dwFlags = KEYEVENTF_KEYUP;
+ SendInput(1, &ip, sizeof(INPUT));
+
+ // Release the "Ctrl" key
+ ip.ki.wVk = VK_CONTROL;
+ ip.ki.dwFlags = KEYEVENTF_KEYUP;
+ SendInput(1, &ip, sizeof(INPUT));
+
+ HANDLE h;
+ Sleep(200);
+ if (!OpenClipboard(NULL))
+ printf("Fail to open the clipboard");
+
+ h = GetClipboardData(CF_TEXT);
+
+ const char* PREFIX = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe \"https://duckduckgo.com/?q=";
+ const char* POSTFIX = "&t=canonical&ia=web\"";
+
+ int size = strlen(PREFIX) + strlen(h) + strlen(POSTFIX) + 1;
+ char *str = malloc(size);
+ strcpy (str, PREFIX);
+ strcat (str, h);
+ strcat (str, POSTFIX);
+
+ CloseClipboard();
+
+ printf("%s\n", (char *)str);
+
+ RunCommand(str);
+}
diff --git a/custfunc.h b/custfunc.h
index ff45195..32aa250 100644
--- a/custfunc.h
+++ b/custfunc.h
@@ -2,5 +2,6 @@
#define CUSTFUNC_H_INCLUDED
void RunCommand(char* command);
+void SearchSelected();
#endif // CUSTFUNC_H_INCLUDED
diff --git a/swhd.c b/swhd.c
index 2f7c3bd..40e5ed6 100644
--- a/swhd.c
+++ b/swhd.c
@@ -46,6 +46,11 @@ void RunKey(int message) {
break;
}
+ if (keys[i].iFunction == FUNCTION_SEARCH) {
+ SearchSelected();
+ break;
+ }
+
// Default program execution
RunCommand(keys[i].path);
break;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment