postmessage说明(postmessage说明)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
postmessage说明(postmessage说明)
Keyboard is a very important input device for our computer. Even today, the mouse is popular, many programs still can not do without keyboard operation. But sometimes, some repetitive, tedious keyboard operation will make people tired, so there is a method with a program to replace the key people, so you can put a lot of repetitive keyboard operation to the program to simulate, save a lot of energy, the wizard button is a software that. So how can we write a program with VB to achieve the function similar to the button Sprite? So let's first get a look at the mechanism that responds to keyboard events in windows.
When the user presses a key on the keyboard, the chip in the keyboard detects the action and sends the signal to the computer. How do you tell which one is pressed? All keys on the keyboard have a code called keyboard scan code. When you press a key, the scan code of this key is passed to the system. Scanning code is related to the specific hardware, the same key, scanning code on different keyboards may be different. The keyboard controller transfers the scan code to the computer, and then gives it to the keyboard driver. The keyboard driver completes the work and converts the scan code to the keyboard virtual code. What is virtual code? Because the scanning code is related to hardware, it is not universal. In order to unify all keys on the keyboard, the concept of virtual code is put forward. No matter what keyboard, the virtual code of the same key is always the same, so that the program can recognize it. Simply put, the virtual code is what we can often see like VK_A, VK_B such constants, such as key A virtual code is 65, written in 16 hexadecimal is &H41, note that people often use 16 binary to represent virtual code. When the keyboard driver converts the
scan code to virtual code, it will transfer the scan code of the keyboard operation with the virtual code and other information to the operating system. The operating system then encapsulates the information in a message and inserts the keyboard message into the message queue. Finally, if no accident, the keyboard message final will be sent to the currently active window where the application window is to receive this message, you know which button on keyboard is pressed, it can make the decision of what response to the user. This process can be represented as follows:
The user presses the button the keyboard driver transfers the event to the operating system the operating system inserts the keyboard event into the message queue the keyboard message is sent to the current active window
Understand this process, we can programming to achieve a link in which to simulate the keyboard operation. In VB, there are a variety of ways to implement keyboard simulations, and we'll introduce several typical ones.
1. local level simulation
From the above process, it can be seen that the keyboard event is finally sent to the active window before it causes the target program to respond. So the most direct way to do this is to forge a keyboard message directly to the target program. Haha, this is really very simple, windows provides several such API function can be achieved directly to the target program to send the message function, commonly used with SendMessage and PostMessage, the difference between them is the PostMessage
function directly to the message to the target program is still on the matter, and send out SendMessage news, but wait for the goal of the program was to return what is good. Here we must note that analog keyboard message must use the PostMessage function to use, SendMessage is not correct (because analog keyboard message is not required to return a value, or the target program will not respond), remember! The VB declaration of the PostMessage function is as follows:
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
The parameter hWnd is the destination of the message you want to send a program on the handle of the control, the parameter wMsg is the type of message that you want to send what kind of message, finally wParam and lParam these two parameters are added with the message data, specific content by news decision.
Let's look at the wMsg parameter, which is the key to the analog button. Keyboard messages are commonly used as follows:
WM_KEYDOWN indicates that a common key is pressed
WM_KEYUP indicates that a common key is released
WM_SYSKEYDOWN indicates that a system key is pressed, such as the Alt key
WM_SYSKEYUP indicates that a system key is released, such as the Alt key
If you're sure you want to send more than one keyboard message, then look at how to determine the two parameters of wParam and lParam in keyboard messages. In a keyboard message, wParam the meaning of the parameters is relatively simple, it represents the key code of virtual keyboard events you want to send, for example you want to target simulation press A key, then the value of the wParam parameter is set to VK_A for lParam, this parameter is more complicated, because it contains more information in general, it can be set to 0, but if you want your simulation more realistic, it is recommended that you still set the parameters. Well, let's get a little more detailed about lParam. LParam is a parameter of type long, it takes 4 bytes in memory, written in binary is 0000000000000000 0000000000000000 total is 32, the number of us from right to left, the right to assume the zeroth (note that it is not from the 0 start counting from 1), the left is thirty-first, then the parameter the 0-15 said the key to send times of extensive information, the 16-23 bit is the key scan code, 24-31 said is to press the key or key release. We generally used written 16 hexadecimal, then it should be 000000 &H00, the 0-15 is generally &H0001, if it is to press the button, then 24-31 is &H00, the release button is &HC0, then the 16-23 scan code do? This requires the use of a API function MapVirtualKey, this function can be transformed into a virtual code scan code, or the scan code into virtual code, virtual code can also be converted to the corresponding character ASCII code. Its VB statement is as follows:
Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long)
As Long
The parameter wCode is to be converted to wMapType code, said from what parameters conversion why, if it is a virtual code to scan code, wMapType is set to 0, if the virtual scan code to virtual code, wMapType is set to 1, if it is a virtual code to ASCII code, wMapType is set to 2. believed the US. You can lParam the structure parameters of keyboard events. A function to construct lParam parameters is given below:
Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
Function MakeKeyLparam (ByVal VirtualKey As Long, ByVal flag As Long) As Long
The parameter VirtualKey represents the virtual code of the key, and the flag represents the press key or the release key, and is represented by the two constants of WM_KEYDOWN and WM_KEYUP
Dim s As String
24-31 bits of Dim Firstbyte As String'lparam parameter
If flag = WM_KEYDOWN Then 'if it is a press key
Firstbyte = "00""
Else
Firstbyte = "C0" if it is the release key
End If
Dim Scancode As Long
'get the key scan code
Scancode = MapVirtualKey (VirtualKey, 0)
16-23 bits of Dim Secondbyte As String'lparam parameter,
即虚拟键扫描码
secondbyte =右(“00”与六(扫描码),2)
S = firstbyte和secondbyte与“0001”“0001为lParam参数的
0-15位,即发送次数和其它扩展信息
makekeylparam = Val(”&“& S)
端功能
这个函数像这样调用,比如按下一键,那么lParam = makekeylparam (vk_a,wm_keydown),很简单吧。
值得注意的是,即使你发送消息时设置了lParam参数的值,但是系统在传递消息时仍然可能会根据当时的情况重新设置该参数,那么目标程序收到的消息中lParam的值可能会和你发送时的有所不同。
所以,如果你很懒的话,还是直接把它设为0吧,对大多数程序不会有影响的,呵呵。
好了,做完以上的事情,现在我们可以向目标程序发送键盘消息了。
首先取得目标程序接受这个消息的控件的句柄,比如目标句柄是12345,那么我们来对目标模拟按下并释放一键,像这样:(为了简单起见,lParam这个参数就不构造了,直接传0)
PostMessage 12345,wm_keydown,vk_a,0和按下一键
PostMessage 12345,wm_up,vk_a,0和释放一键
好了,一次按键就完成了。
现在你可以迫不及待的打开记事本做实验,先用FindWindowEx这类API函数找到记事本程序的句柄,再向它发送键盘消息,期望记事本里能诡异的自动出现字符。
可是你马上就是失望了,咦,怎么一点反应也没有?你欺骗感情啊~~~~~~~~~~ 55555555555555不是的哦,接着往下看啊。
一般目标程序都会含有多个控件,并不是每个控件都会对键盘消息作出反应,只有把键盘消息发送给接受它的控件才会得到期望的反应。
那记事本来说,它的编辑框其实是个编辑类,只有这个控件才对键盘事件有反应,如果只是把消息发给记事本的窗体,那是没有用的。
现在你找出记事本那个编辑框的句柄,比如是54321,那么写如下代码:
PostMessage 54321,wm_keydown,vk_f1,0和按下F1键
PostMessage 54321,wm_up,vk_f1,0和释放F1键
怎么样,是不是打开了记事本的”帮助”信息?这说明目标程序已经收到了你发的消息,还不错吧~~~~~~~~
可以马上新问题就来了,你想模拟向记事本按下一这个键,好在记事本里自动输入字符,可是,没有任何反应!这是怎么一回事呢?
原来,如果要向目标程序发送字符,光靠wm_keydown和wm_up这两
个事件还不行,还需要一个事件:wm_char,这个消息表示一个字符,程序需靠它看来接受输入的字符。
一般只有,B、C等这样的按键才有wm_char消息,别的键(比如方向键和功能键)是没有这个消息的,wm_char消息一般发生在wm_keydown消息之后。
wm_char消息的lParam参数的含义与其它键盘消息一样,而它的wParam则表示相应字符的ASCII编码(可以输入中文的哦^ _ ^),现在你可以写出一个完整的向记事本里自动写入字符的程序了,下面是一个例子,并附有这些消息常数的具体值:
声明函数PostMessage lib“user32”别名“postmessagea”(ByVal hwnd为长,ByVal wMsg为长,ByVal wParam为长,指向任何)长
函数声明mapvirtualkey lib“user32”别名“mapvirtualkeya”(ByVal wCode为长,
一个wmaptype长)长
公共建设wm_keydown =和H100
公共建设wm_keyup =与H101
公共建设wm_char = & H102
公共建设vk_a =和H41
函数的MakeKeyLparam(ByVal VirtualKey为长,ByVal国旗长长)
以s为字符串
昏暗的firstbyte作为字符串指向参数的位24-31
如果标志为wm_keydown然后如果是按下键
firstbyte =“00”
其他的
firstbyte =“C0”如果是释放键
最后如果
朦胧的Scancode As Long
“获得键的扫描码
扫描码= mapvirtualkey(virtualkey,0)
昏暗的secondbyte作为字符串指向参数的16-23位,即虚拟键扫描码
secondbyte =右(“00”与六(扫描码),2)
S = firstbyte和secondbyte与“0001”“0001为lParam参数的0-15位,即发送次数和其它扩展信息
makekeylparam = Val(”&“& S)
端功能
私有子form_load()
昏暗的窗口句柄
“xxxxx表示记事本编辑框的句柄xxxxxx hwnd =
PostMessage HWND,wm_keydown,vk_a,MakeKeyLparam(vk_a,wm_keydown)的按下一键
PostMessage HWND,wm_char,ASC(“”)、MakeKeyLparam(vk_a,wm_keydown)的输入字符一
PostMessage HWND,wm_up,vk_a,MakeKeyLparam(vk_a,wm_up)的释放一键
端子
这就是通过局部键盘消息来模拟按键。
这个方法有一个极大的好处,就是:它可以实现后台按键,也就是说他对你的前台操作不会有什么影响。
比如,你可以用这个方法做个程序在游戏中模拟按键来不断地执行某些重复的操作,而你则一边喝茶一边与QQ上的毫米们聊得火热,它丝毫不会影响你的前台操作。
无论目标程序是否获得焦点都没有影响,这就是后台模拟按键的原理啦~ ~ ~ ~。