Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 2147
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 31, 20222022-05-31T16:19:25+00:00 2022-05-31T16:19:25+00:00

c++ – Intercept keyboard and replace characters

  • 0

[ad_1]

I’m trying to make my native language’s keyboard layout and converter. Where I’ll type something in english and the output characters will be something else from my native language character. But some times I need to update the textbox [where I’ll use the program to write] for one keystroke to 2-3 characters. Like if I type h the output will be n but if I type ho the Output will be ‡nv. Here one character will be appended before and after n.
We have our own keyboard software which has its own layout where we have to use 50 characters. So I wished to make a program that will help me and others to type fast using english.

E.G: ‡nv. is the output what we actually get from our own software. But we need a special font to render this word to something meaningful to us.

This is a sample program I had tried.

#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include <iostream>
#include <unordered_map>
#include <queue>
#include <thread>

using namespace std::chrono_literals;

HHOOK hHook{ NULL };

enum Keys
{
    ShiftKey = 16,
    Capital = 20,
};

//create a map
std::unordered_map<char, int> keyMap;
std::queue<char> lastPressedKeys;


int shift_active() {
    return GetKeyState(VK_LSHIFT) < 0 || GetKeyState(VK_RSHIFT) < 0;
}

int capital_active() {
    return (GetKeyState(VK_CAPITAL) & 1) == 1;
}

LRESULT CALLBACK keyboard_hook(const int code, const WPARAM wParam, const LPARAM lParam) {
    if (wParam == WM_KEYDOWN) {
        KBDLLHOOKSTRUCT* kbdStruct = (KBDLLHOOKSTRUCT*)lParam;
        DWORD wVirtKey = kbdStruct->vkCode;
        DWORD wScanCode = kbdStruct->scanCode;
        BYTE lpKeyState[256];
        GetKeyboardState(lpKeyState);
        lpKeyState[Keys::ShiftKey] = 0;
        lpKeyState[Keys::Capital] = 0;
        if (shift_active()) {
            lpKeyState[Keys::ShiftKey] = 0x80;
        }
        if (capital_active()) {
            lpKeyState[Keys::Capital] = 0x01;
        }
        char result = 0;        
        ToAscii(wVirtKey, wScanCode, lpKeyState, (LPWORD)&result, 0);
        if (keyMap[result])
        {
            if (lastPressedKeys.size() >= 3)
            {
                lastPressedKeys.pop();
            }
            lastPressedKeys.push(result);
            std::cout << "Last 3 pressed keys: {";
            //loop through the queue
            std::queue<char> temp = lastPressedKeys;
            while (!temp.empty())
            {
                std::cout << temp.front() << " ";
                temp.pop();
            }
            std::cout << " }\n";
            std::cout << "Pressed Key: " << result+1 << std::endl;
            //simulate key press
            keybd_event(toupper(result), 0, KEYEVENTF_EXTENDEDKEY, 0);
            keybd_event(toupper(result), 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
            return 1;
        }
        else if (result == ' ' || result == 13)
        {
            //empty lastpressedKey
            while (!lastPressedKeys.empty()) lastPressedKeys.pop();
        }
    }
    return CallNextHookEx(hHook, code, wParam, lParam);
}

void InitKeyMap()
{
    keyMap['a'] = true;
    keyMap['b'] = true;
    keyMap['c'] = true;
    keyMap['d'] = true;
    keyMap['e'] = true;
    keyMap['f'] = true;
    keyMap['g'] = true;
    keyMap['h'] = true;
    keyMap['i'] = true;
    keyMap['j'] = true;
    keyMap['k'] = true;
    keyMap['l'] = true;
    keyMap['m'] = true;
    keyMap['n'] = true;
    keyMap['o'] = true;
    keyMap['p'] = true;
    keyMap['q'] = true;
    keyMap['r'] = true;
    keyMap['s'] = true;
    keyMap['t'] = true;
    keyMap['u'] = true;
    keyMap['v'] = true;
    keyMap['w'] = true;
    keyMap['x'] = true;
    keyMap['y'] = true;
    keyMap['z'] = true;
    keyMap['A'] = true;
    keyMap['B'] = true;
    keyMap['C'] = true;
    keyMap['D'] = true;
    keyMap['E'] = true;
    keyMap['F'] = true;
    keyMap['G'] = true;
    keyMap['H'] = true;
    keyMap['I'] = true;
    keyMap['J'] = true;
    keyMap['K'] = true;
    keyMap['L'] = true;
    keyMap['M'] = true;
    keyMap['N'] = true;
    keyMap['O'] = true;
    keyMap['P'] = true;
    keyMap['Q'] = true;
    keyMap['R'] = true;
    keyMap['S'] = true;
    keyMap['T'] = true;
    keyMap['U'] = true;
    keyMap['V'] = true;
    keyMap['W'] = true;
    keyMap['X'] = true;
    keyMap['Y'] = true;
    keyMap['Z'] = true;
    keyMap['0'] = true;
    keyMap['1'] = true;
    keyMap['2'] = true;
    keyMap['3'] = true;
    keyMap['4'] = true;
    keyMap['5'] = true;
    keyMap['6'] = true;
    keyMap['7'] = true;
    keyMap['8'] = true;
    keyMap['9'] = true;
}

int main(int argc, char* argv[])
{
    InitKeyMap();
    
    std::cout << "Key Initialized!\n"; 
    hHook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboard_hook, NULL, 0);
    if (hHook == NULL) {
        std::cout << "Keyboard hook failed!" << std::endl;
    }

    while (GetMessage(NULL, NULL, 0, 0));
    return 0;
}

I need something that I write ho on the keyboard and the output will be ‡nv without ho but only ‡nv

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 5256 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1098 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1047 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.