WM_KEYDOWN和WM_KEYUP的使用

通过这两个消息可以模拟键盘事件。

相关函数:

MapVirtualKey: The MapVirtualKey function translates (maps) a virtual-key code into a scan code or character value, or translates a scan code into a virtual-key code.

VkKeyScan: The VkKeyScan function translates a character to the corresponding virtual-key code and shift state for the current keyboard.

PostMessage,SetForegroundWindow

 

WM_KEYDOWN和WM_KEYUP的 wParam就是虚拟键码,MSDN上可以查到,也可以通过VkKeyScan将一个字符转换成虚拟键码和shift状态的结合。

lParam的0到15位为该键在键盘上的重复次数,经常设为1,即按键1次;16至23位为键盘的扫描码,通过MapVirtualKey配合其参数可以得到;24位为扩展键,即某些右ALT和CTRL;29、30、31位按照说明设置即可(第30位对于keydown在和shift等结合的时候通常要设置为1)。

 

Specification of WM_KEYDOWN:

wParam

Specifies the virtual-key code of the nonsystem key.
lParam
Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table.
0-15
Specifies the repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. If the keystroke is held long enough, multiple messages are sent. However, the repeat count is not cumulative.
16-23
Specifies the scan code. The value depends on the OEM.
24
Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.
25-28
Reserved; do not use.
29
Specifies the context code. The value is always 0 for a WM_KEYDOWN message.
30
Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is zero if the key is up.
31
Specifies the transition state. The value is always zero for a WM_KEYDOWN message.
Specification of WM_KEYUP:
wParam
Specifies the virtual-key code of the nonsystem key.
lParam
Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table.
0-15
Specifies the repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. The repeat count is always one for a WM_KEYUP message.
16-23
Specifies the scan code. The value depends on the OEM.
24
Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.
25-28
Reserved; do not use.
29
Specifies the context code. The value is always 0 for a WM_KEYUP message.
30
Specifies the previous key state. The value is always 1 for a WM_KEYUP message.
31
Specifies the transition state. The value is always 1 for a WM_KEYUP message.

Examples:

 

1 char ch = ...;
2 SHORT tmp = VkKeyScan(ch);
3 WPARAM wParam = tmp & 0xFF;
4 LPARAM lParam = 1;
5 lParam += MapVirtualKey(wParam, MAPVK_VK_TO_VSC) << 16;
6  bool shift = (tmp & 0x0100) == 0x0100 ? true : false;
7  //未考虑SHIFT,大小写的区分需通过SHIFT,注意设置第29位,可用Spy++捕获消息查看,此处只作备忘
8  PostMessage(h_wnd, WM_KEYDOWN, wParam, lParam);
9 lParam += 1 << 30;
10 lParam += 1 << 31;
11 PostMessage(h_wnd, WM_KEYUP, wParam, lParam);

 

 

用keybd_event实现:

 

1 char ch = ...;
2 SHORT tmp = VkKeyScan(ch);
3 BYTE vk = tmp & 0xFF;
4 bool shift = (tmp & 0x0100) == 0x0100 ? true : false;
5 if (shift) {
6 keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, MAPVK_VK_TO_VSC), 0, 0);
7 }
8 keybd_event(vk, MapVirtualKey(vk, MAPVK_VK_TO_VSC), 0, 0);
9 keybd_event(vk, MapVirtualKey(vk, MAPVK_VK_TO_VSC), KEYEVENTF_KEYUP, 0);
10 if (shift) {
11 keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, MAPVK_VK_TO_VSC), KEYEVENTF_KEYUP, 0);
12 }

 

参考:Big problem with simulating keypresses

posted on 2010-12-17 22:00  lbsx  阅读(39825)  评论(0编辑  收藏  举报