0

我试图让我的应用程序输出一个组合键 ( 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。

4

1 回答 1

3

将加速器映射添加到您的项目资源中,在运行时将其加载到您的应用程序中,并在您的消息循环中添加对 TranslateAccelerator 的调用,然后 TranslateMessage 和 DispatchMessage 有机会查看它。

http://msdn.microsoft.com/en-us/library/windows/desktop/ms646373%28v=vs.85%29.aspx供参考。

于 2012-05-10T22:05:28.900 回答