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

转载于:https://www.cnblogs.com/lbsx/archive/2010/12/17/1909639.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
消息,就是指Windows发出的一个通知,告诉应用程序某个事情发生了。例如,单击鼠标、改变窗口尺寸、按下键盘上的一个键都会使Windows发送一个消息给应用程序。 消息本身是作为一个记录传递给应用程序的,这个记录中包含了消息的类型以及其他信息。例如,对于单击鼠标所产生的消息来说,这个记录中包含了单击鼠标时的坐标。这个记录类型叫做TMsg,它在Windows单元中是这样声明的: type TMsg = packed record hwnd: HWND; //窗口句柄 message: UINT;//消息常量标识符 wParam: WPARAM ;// 32位消息的特定附加信息 lParam: LPARAM ;// 32位消息的特定附加信息 time: DWORD;//消息创建时的时间 pt: TPoint; //消息创建时的鼠标位置 end ; 消息中有什么? 是否觉得一个消息记录中的信息像希腊语一样?如果是这样,那么看一看下面的解释:hwnd 32位的窗口句柄。窗口可以是任何类型的屏幕对象,因为Win32能够维护大多数可 视对象的句柄(窗口、对话框、按钮、编辑框等)。message 用于区别其他消息的常量值,这些常量可以是Windows单元中预定义的常量,也 可以是自定义的常量。 wParam 通常是一个与消息有关的常量值,也可能是窗口或控件的句柄。 lParam 通常是一个指向内存中数据的指针。由于WParam、lParam和Pointer都是32位的,因此,它们之间可以相互转换。 WM_NULL =$0000 // WM_CREATE =$0001 //应用程序创建一个窗口 WM_DESTROY = $0002 //一个窗口被销毁 WM_MOVE = $0003 //移动一个窗口 WM_SIZE= $0005 //改变一个窗口的大小 WM_ACTIVATE= $0006 //一个窗口被激活或失去激活状态; WM_SETFOCUS= $0007 //获得焦点后 WM_KILLFOCUS= $0008 //失去焦点 WM_ENABLE= $000A //改变enable状态 WM_SETREDRAW= $000B //设置窗口是否能重画 WM_SETTEXT= $000C //应用程序发送此消息来设置一个窗口的文本 WM_GETTEXT = $000D //应用程序发送此消息来复制对应窗口的文本到缓冲区 WM_GETTEXTLENGTH = $000E //得到与一个窗口有关的文本的长度(不包含空字符) WM_PAINT = $000F //要求一个窗口重画自己 WM_CLOSE = $0010 //当一个窗口或应用程序要关闭时发送一个信号 WM_QUERYENDSESSION= $0011 //当用户选择结束对话框或程序自己调用ExitWindows函数 WM_QUIT= $0012 //用来结束程序运行或当程序调用postquitmessage函数 WM_QUERYOPEN = $0013 //当用户窗口恢复以前的大小位置时,把此消息发送给某个图标 WM_ERASEBKGND = $0014 //当窗口背景必须被擦除时(例在窗口改变大小时) WM_SYSCOLORCHANGE = $0015 //当系统颜色改变时,发送此消息给所有顶级窗口 WM_ENDSESSION = $0016 // 当系统进程发出WM_QUERYENDSESSION消息后,此消息发送给应用程序,通知它对话是否结束 WM_SYSTEMERROR = $0017 // WM_SHOWWINDOW= $0018 //当隐藏或显示窗口是发送此消息给这个窗口 WM_ACTIVATEAPP = $001C //发此消息给应用程序哪个窗口是激活的,哪个是非激活的; WM_FONTCHANGE= $001D //当系统的字体资源库变化时发送此消息给所有顶级窗口 WM_TIMECHANGE= $001E //当系统的时间变化时发送此消息给所有顶级窗口 WM_CANCELMODE= $001F //发送此消息来取消某种正在进行的摸态(操作) WM_SETCURSOR = $0020 //如果鼠标引起光标在某个窗口中移动且鼠标输入没有被捕获时,就发消息给某个窗口 WM_MOUSEACTIVATE = $0021 //当光标在某个非激活的窗口中而用户正按着鼠标的某个键发送此消息给当前窗口 WM_CHILDACTIVATE = $0022 //发送此消息给MDI子窗口当用户点击此窗口的标题栏,或当窗口被激活,移动,改变大小 WM_QUEUESYNC= $0023 //此消息由基

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值