我试图让我的应用程序输出一个组合键 ( ALT + D ) 以专注于 Internet Explorer 地址栏,但我无法实现所需的代码。我已经有一个方法来传递 1 个密钥:
void GenerateKey(int vk, BOOL bExtended) {
KEYBDINPUT kb = {0};
INPUT Input = {0};
/* Generate a "key down" */
if (bExtended) { kb.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));
/* Generate a "key up" */
ZeroMemory(&kb, sizeof(KEYBDINPUT));
ZeroMemory(&Input, sizeof(INPUT));
kb.dwFlags = KEYEVENTF_KEYUP;
if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));
return;
}
任何人都可以就如何实现所需的解决方案提供一些帮助吗?
解决方案:
我设法使用以下方法解决了这个问题:
void GenerateKeyCombination(int vk, int vk2, BOOL bExtended, BOOL bExtended2) {
KEYBDINPUT kb = {0};
INPUT Input = {0};
KEYBDINPUT kb2 = {0};
INPUT Input2 = {0};
// Generate a "key down" 1
if (bExtended) { kb.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));
// Generate a "key down" 2
if (bExtended2) { kb2.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb2.wVk = vk2;
Input2.type = INPUT_KEYBOARD;
Input2.ki = kb2;
SendInput(1, &Input2, sizeof(Input2));
// Generate a "key up" 1
ZeroMemory(&kb, sizeof(KEYBDINPUT));
ZeroMemory(&Input, sizeof(INPUT));
kb.dwFlags = KEYEVENTF_KEYUP;
if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));
// Generate a "key up" 2
ZeroMemory(&kb2, sizeof(KEYBDINPUT));
ZeroMemory(&Input2, sizeof(INPUT));
kb2.dwFlags = KEYEVENTF_KEYUP;
if (bExtended2) { kb2.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb2.wVk = vk2;
Input2.type = INPUT_KEYBOARD;
Input2.ki = kb2;
SendInput(1, &Input2, sizeof(Input2));
return;
}
并这样称呼它:
GenerateKeyCombination(0x12, 0x44, FALSE, FALSE);
其中 0x12 是 ALT,0x44 是 D。