Created
June 26, 2025 21:00
-
-
Save rbmm/3e584c9fc91882a6c3079d05cb6a432d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define SHORT_SIZE (sizeof(USHORT)) | |
#define SHORT_MASK (SHORT_SIZE - 1) | |
#define LONG_SIZE (sizeof(LONG)) | |
#define LONGLONG_SIZE (sizeof(LONGLONG)) | |
#define LONG_MASK (LONG_SIZE - 1) | |
#define LONGLONG_MASK (LONGLONG_SIZE - 1) | |
#define LOWBYTE_MASK 0x00FF | |
#define FIRSTBYTE(VALUE) ((VALUE) & LOWBYTE_MASK) | |
#define SECONDBYTE(VALUE) (((VALUE) >> 8) & LOWBYTE_MASK) | |
#define THIRDBYTE(VALUE) (((VALUE) >> 16) & LOWBYTE_MASK) | |
#define FOURTHBYTE(VALUE) (((VALUE) >> 24) & LOWBYTE_MASK) | |
void Fill(PINPUT pi, BYTE wVk, DWORD dwFlags) | |
{ | |
pi->type = INPUT_KEYBOARD; | |
pi->ki.wVk = wVk; | |
pi->ki.wScan = (WORD)MapVirtualKey(wVk, MAPVK_VK_TO_VSC); | |
pi->ki.dwFlags = dwFlags; | |
pi->ki.time = 0; | |
} | |
void SendString(PCWSTR psz) | |
{ | |
if (ULONG len = (ULONG)wcslen(psz)) | |
{ | |
ULONG cb = len * sizeof(INPUT) << 2; | |
if (PVOID buf = _malloca(cb)) | |
{ | |
ULONG bShiftPressed = 0; | |
PINPUT pi = (PINPUT)buf; | |
ULONG cInputs = 0; | |
do | |
{ | |
SHORT s = VkKeyScan(*psz++); | |
if (0 > s) | |
{ | |
continue; | |
} | |
if ((SECONDBYTE(s) & 1) ^ bShiftPressed) | |
{ | |
Fill(pi++, VK_SHIFT, (bShiftPressed = 1 - bShiftPressed) ? 0 : KEYEVENTF_KEYUP); | |
cInputs++; | |
} | |
Fill(pi, FIRSTBYTE(s), 0); | |
pi[1] = *pi; | |
(++pi)->ki.dwFlags = KEYEVENTF_KEYUP; | |
pi++, cInputs += 2; | |
} while (--len); | |
if (bShiftPressed) | |
{ | |
Fill(pi, VK_SHIFT, KEYEVENTF_KEYUP); | |
cInputs++; | |
} | |
if (cInputs) | |
{ | |
SendInput(cInputs, (PINPUT)buf, sizeof(INPUT)); | |
} | |
_freea(buf); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment