[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]