Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Public Discord: http://public.FutureGadgetLab.net (Alaestor FGL 2017)
- /*****************************************************************************
- * Copywrong (C) 1337-9001 Hooin Kyoma <Kyoma@FutureGadgetLab.fake> *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 9 of the License, or *
- * (at your option) any license you want. I dun give fucks. *
- * *
- * This program is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * LONGCAT IS LONG or FITNESS FOR A PATRIARCHAL PURPOSE. See the *
- * GNU General Public License for more dank memes. *
- * *
- * You shouldn't have received a copy of the GNU General Public License *
- * with this program; if not, dont write to the Free Software Foundation *
- * because you may be retarded. Think fast douche-fag. *
- * *
- * Divergence 1.048596, Future Gadget Lab, Chiyoda-ku KuramaeBashi Douri *
- *****************************************************************************/
- /*
- Simple "key remapper" program for windows.
- Suppresses original key press if the key is remapped.
- Simulated keys were tested on both direct and raw input.
- Must be run as admin to effect other admin-level processes.
- */
- #define WINVER _WIN32_WINNT_WIN7
- #include <cstdio>
- #include <string>
- #include <vector>
- #include <windows.h>
- #include <winuser.h> // should be redundent
- // enable this for spam :D
- static constexpr bool debug_printing = false;
- /**************************
- printing functions
- ***************************/
- char cGetChar()
- {
- // Get the first char of the user input
- char first = getchar();
- // empty the buffer by reading from it until the end
- register char c;
- while ((c = getchar()) != '\n' && c != EOF);
- return first;
- }
- void log(const std::string& msg)
- {
- printf(" %s\n", msg.c_str());
- }
- int error(const std::string& msg)
- {
- printf(" Error: %s\n", msg.c_str());
- puts("press ENTER to terminate");
- cGetChar();
- return 0;
- }
- /**************************
- managing objects
- ***************************/
- class Record
- {
- public:
- std::vector<unsigned char> in;
- std::vector<unsigned char> out;
- bool pairCheck()
- {
- if (in.size() != out.size()) return error("Pair check failed!");
- else return true;
- }
- int findIndexFor(const unsigned char vkey_in)
- {
- const std::size_t elements = in.size();
- for (unsigned int i = 0; i < elements; i++)
- if (vkey_in == in[i]) return i;
- return (-1);
- }
- void associate(
- const unsigned char vkey_in,
- const unsigned char vkey_out
- )
- {
- in.push_back(vkey_in);
- out.push_back(vkey_out);
- }
- bool disassociate(const unsigned char vkey_in)
- {
- const int i = findIndexFor(vkey_in);
- if (i < 0) return error("disassociation index not found!");
- in.erase(in.begin() + i);
- out.erase(out.begin() + i);
- return true;
- }
- Record()
- {
- constexpr std::size_t reserved_elements = 10;
- in.reserve(reserved_elements);
- out.reserve(reserved_elements);
- }
- ~Record() = default;
- };
- static class KeyEventManager
- {
- private:
- Record key;
- public:
- auto output(const unsigned char vkey, const bool downPress)
- { // simulate keyboard input
- if (debug_printing)
- {
- char buff[50];
- snprintf(buff, sizeof(buff),
- "output:\tvkey %02x %s (from remap)",
- vkey, downPress ? "down" : "up"
- );
- log(buff);
- }
- INPUT in;
- in.type = INPUT_KEYBOARD;
- in.ki.wVk = vkey;
- in.ki.wScan = MapVirtualKey(vkey, MAPVK_VK_TO_VSC);
- in.ki.dwFlags = downPress ? 0 : KEYEVENTF_KEYUP;
- in.ki.dwExtraInfo = 0;
- in.ki.time = 0;
- return SendInput(1, &in, sizeof(in));
- }
- bool input(const unsigned char vkey, const bool downPress)
- { // input behavior
- const int i = key.findIndexFor(vkey);
- if (i < 0)
- {
- if (debug_printing)
- {
- char buff[50];
- snprintf(buff, sizeof(buff),
- "input: \tvkey %02x %s",
- vkey, downPress ? "down" : "up"
- );
- log(buff);
- }
- return false;
- }
- else if (debug_printing)
- {
- char buff[50];
- snprintf(buff, sizeof(buff),
- "input: \tvkey %02x %s (remapped)",
- vkey, downPress ? "down" : "up"
- );
- log(buff);
- }
- output(key.out[i], downPress);
- return true;
- }
- void remap(
- const unsigned char vkey_in,
- const unsigned char vkey_out
- )
- { // make a remap, associate a key in with a key out
- if (debug_printing)
- {
- char buff[50];
- snprintf(buff, sizeof(buff),
- "Remap created: vkey %02x to %02x",
- vkey_in, vkey_out
- );
- log(buff);
- }
- key.associate(vkey_in, vkey_out);
- }
- bool remapDelete(const unsigned char vkey)
- { // destroy a remap, disassociate a key in with a key out
- if (debug_printing)
- {
- char buff[50];
- snprintf(buff, sizeof(buff),
- "Remap deleted: vkey %02x", vkey);
- log(buff);
- }
- return key.disassociate(vkey);
- }
- KeyEventManager() = default;
- ~KeyEventManager() = default;
- } keyEvent;
- /**************************
- windows callback
- ***************************/
- LRESULT CALLBACK llKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
- {
- if (nCode == HC_ACTION)
- {
- KBDLLHOOKSTRUCT& kb = *reinterpret_cast<KBDLLHOOKSTRUCT *>(lParam);
- const unsigned char vkey = kb.vkCode;
- const bool state = (wParam == WM_KEYDOWN);
- const bool remapped = keyEvent.input(vkey, state);
- if (remapped) return -1; // supress original input
- }
- return CallNextHookEx(nullptr, nCode, wParam, lParam);
- }
- /**************************
- Proc elevation check
- ***************************/
- bool elevated(HANDLE process)
- {
- HANDLE h = INVALID_HANDLE_VALUE;
- TOKEN_ELEVATION te;
- if (OpenProcessToken(process, TOKEN_QUERY, &h))
- {
- unsigned long cbSize = sizeof(TOKEN_ELEVATION);
- GetTokenInformation(h, TokenElevation, &te, sizeof(te), &cbSize);
- if (h != INVALID_HANDLE_VALUE) CloseHandle(h);
- }
- return te.TokenIsElevated ? true : false;
- }
- /**************************
- Primary Functions
- ***************************/
- void remap_keys()
- { // specify virtual key remaps here
- keyEvent.remap(VK_F20, VK_NUMPAD7);
- keyEvent.remap(VK_F21, VK_NUMPAD8);
- keyEvent.remap(VK_F24, VK_NUMPAD9);
- }
- int main()
- {
- // make sure we're running as admin, because we should be
- if ( !elevated(GetCurrentProcess()) )
- return error("Key Remapper must be run as administrator");
- log("Hardcoded Key Remapper STARTED");
- remap_keys();
- HHOOK keyboardHook = SetWindowsHookEx(
- WH_KEYBOARD_LL,
- llKeyboardProc,
- GetModuleHandle("kernel32.dll"),
- 0
- );
- if (keyboardHook==nullptr) return error("SetWindowsHook() failed");
- MSG msg;
- while ( !GetMessage(&msg, nullptr, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- UnhookWindowsHookEx(keyboardHook);
- log("Hardcoded Key Remapper ENDED");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement