C 键盘事件
C keyboard events
这是我第一次使用 C 在 Windows API 中编程。我制作了这个简单的程序,它接受一个字符串并在您的计算机上再次输入它。我认为这可以更好地实现,因为我有很多 if
和 else if
语句。我的问题是:这是构建这样一个程序的好方法吗?有更好的方法吗?
#include <stdio.h>
#include <windows.h>
void keyboard(char *str)
{
int x = strlen(str);
INPUT inputStruct[x];
ZeroMemory(inputStruct, sizeof(inputStruct));
for (int i = 0; i < x; i++)
{
inputStruct[i].type = 1;
if (str[i] == 'a')
inputStruct[i].ki.wVk = 0x41;
else if (str[i] == 'b')
inputStruct[i].ki.wVk = 0x42;
else if (str[i] == 'c')
inputStruct[i].ki.wVk = 0x43;
else if (str[i] == 'd')
inputStruct[i].ki.wVk = 0x44;
else if (str[i] == 'e')
inputStruct[i].ki.wVk = 0x45;
else if (str[i] == 'f')
inputStruct[i].ki.wVk = 0x46;
else if (str[i] == 'g')
inputStruct[i].ki.wVk = 0x47;
else if (str[i] == 'h')
inputStruct[i].ki.wVk = 0x48;
else if (str[i] == 'i')
inputStruct[i].ki.wVk = 0x49;
else if (str[i] == 'j')
inputStruct[i].ki.wVk = 0x4A;
else if (str[i] == 'k')
inputStruct[i].ki.wVk = 0x4B;
else if (str[i] == 'l')
inputStruct[i].ki.wVk = 0x4C;
else if (str[i] == 'm')
inputStruct[i].ki.wVk = 0x4D;
else if (str[i] == 'n')
inputStruct[i].ki.wVk = 0x4E;
else if (str[i] == 'o')
inputStruct[i].ki.wVk = 0x4F;
else if (str[i] == 'p')
inputStruct[i].ki.wVk = 0x50;
else if (str[i] == 'q')
inputStruct[i].ki.wVk = 0x51;
else if (str[i] == 'r')
inputStruct[i].ki.wVk = 0x52;
else if (str[i] == 's')
inputStruct[i].ki.wVk = 0x53;
else if (str[i] == 't')
inputStruct[i].ki.wVk = 0x54;
else if (str[i] == 'u')
inputStruct[i].ki.wVk = 0x55;
else if (str[i] == 'v')
inputStruct[i].ki.wVk = 0x56;
else if (str[i] == 'w')
inputStruct[i].ki.wVk = 0x57;
else if (str[i] == 'x')
inputStruct[i].ki.wVk = 0x58;
else if (str[i] == 'y')
inputStruct[i].ki.wVk = 0x59;
else if (str[i] == 'z')
inputStruct[i].ki.wVk = 0x5A;
else if (str[i] == ' ')
inputStruct[i].ki.wVk = 0x20;
}
SendInput(x, inputStruct, sizeof(INPUT));
}
int main()
{
keyboard("this is my test program");
}
您可以使用
VkKeyScan
为了将您的字符转换为虚拟键码
那就试试吧
inputStruct[i].ki.wVk = VkKeyScan(str[i]);
而不是像下面这样的一堆 if 子句
if (str[i] == ' ')
inputStruct[i].ki.wVk = 0x20
所以最终代码如下所示
for (int i = 0; i < x; i++)
{
inputStruct[i].type = 1;
inputStruct[i].ki.wVk = VkKeyScan(str[i]);
}
您可以在发送键之前先按 shift,然后发送您希望按大写形式按下的键
下面的代码是您的第二个目的的演示
INPUT inputs[4] = { 0 };
ZeroMemory(inputs, sizeof(inputs));
inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki.wVk = VK_LSHIFT;
inputs[1].type = INPUT_KEYBOARD;
inputs[1].ki.wVk = VkKeyScan(L'a');
inputs[2].type = INPUT_KEYBOARD;
inputs[2].ki.wVk = VkKeyScan(L'a');
inputs[2].ki.dwFlags = KEYEVENTF_KEYUP;
inputs[3].type = INPUT_KEYBOARD;
inputs[3].ki.wVk = VK_LSHIFT;
inputs[3].ki.dwFlags = KEYEVENTF_KEYUP;
UINT uSent = SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
这是我第一次使用 C 在 Windows API 中编程。我制作了这个简单的程序,它接受一个字符串并在您的计算机上再次输入它。我认为这可以更好地实现,因为我有很多 if
和 else if
语句。我的问题是:这是构建这样一个程序的好方法吗?有更好的方法吗?
#include <stdio.h>
#include <windows.h>
void keyboard(char *str)
{
int x = strlen(str);
INPUT inputStruct[x];
ZeroMemory(inputStruct, sizeof(inputStruct));
for (int i = 0; i < x; i++)
{
inputStruct[i].type = 1;
if (str[i] == 'a')
inputStruct[i].ki.wVk = 0x41;
else if (str[i] == 'b')
inputStruct[i].ki.wVk = 0x42;
else if (str[i] == 'c')
inputStruct[i].ki.wVk = 0x43;
else if (str[i] == 'd')
inputStruct[i].ki.wVk = 0x44;
else if (str[i] == 'e')
inputStruct[i].ki.wVk = 0x45;
else if (str[i] == 'f')
inputStruct[i].ki.wVk = 0x46;
else if (str[i] == 'g')
inputStruct[i].ki.wVk = 0x47;
else if (str[i] == 'h')
inputStruct[i].ki.wVk = 0x48;
else if (str[i] == 'i')
inputStruct[i].ki.wVk = 0x49;
else if (str[i] == 'j')
inputStruct[i].ki.wVk = 0x4A;
else if (str[i] == 'k')
inputStruct[i].ki.wVk = 0x4B;
else if (str[i] == 'l')
inputStruct[i].ki.wVk = 0x4C;
else if (str[i] == 'm')
inputStruct[i].ki.wVk = 0x4D;
else if (str[i] == 'n')
inputStruct[i].ki.wVk = 0x4E;
else if (str[i] == 'o')
inputStruct[i].ki.wVk = 0x4F;
else if (str[i] == 'p')
inputStruct[i].ki.wVk = 0x50;
else if (str[i] == 'q')
inputStruct[i].ki.wVk = 0x51;
else if (str[i] == 'r')
inputStruct[i].ki.wVk = 0x52;
else if (str[i] == 's')
inputStruct[i].ki.wVk = 0x53;
else if (str[i] == 't')
inputStruct[i].ki.wVk = 0x54;
else if (str[i] == 'u')
inputStruct[i].ki.wVk = 0x55;
else if (str[i] == 'v')
inputStruct[i].ki.wVk = 0x56;
else if (str[i] == 'w')
inputStruct[i].ki.wVk = 0x57;
else if (str[i] == 'x')
inputStruct[i].ki.wVk = 0x58;
else if (str[i] == 'y')
inputStruct[i].ki.wVk = 0x59;
else if (str[i] == 'z')
inputStruct[i].ki.wVk = 0x5A;
else if (str[i] == ' ')
inputStruct[i].ki.wVk = 0x20;
}
SendInput(x, inputStruct, sizeof(INPUT));
}
int main()
{
keyboard("this is my test program");
}
您可以使用
VkKeyScan
为了将您的字符转换为虚拟键码
那就试试吧
inputStruct[i].ki.wVk = VkKeyScan(str[i]);
而不是像下面这样的一堆 if 子句
if (str[i] == ' ')
inputStruct[i].ki.wVk = 0x20
所以最终代码如下所示
for (int i = 0; i < x; i++)
{
inputStruct[i].type = 1;
inputStruct[i].ki.wVk = VkKeyScan(str[i]);
}
您可以在发送键之前先按 shift,然后发送您希望按大写形式按下的键
下面的代码是您的第二个目的的演示
INPUT inputs[4] = { 0 };
ZeroMemory(inputs, sizeof(inputs));
inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki.wVk = VK_LSHIFT;
inputs[1].type = INPUT_KEYBOARD;
inputs[1].ki.wVk = VkKeyScan(L'a');
inputs[2].type = INPUT_KEYBOARD;
inputs[2].ki.wVk = VkKeyScan(L'a');
inputs[2].ki.dwFlags = KEYEVENTF_KEYUP;
inputs[3].type = INPUT_KEYBOARD;
inputs[3].ki.wVk = VK_LSHIFT;
inputs[3].ki.dwFlags = KEYEVENTF_KEYUP;
UINT uSent = SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));