- ardim
-
keybd_event 是模拟键盘动作的API过程,即只能向当前激活的窗口模拟键盘动作,如果要向非激活的窗口发送键盘信息的话只能使用其他的API函数,下面我的程序就模拟了运行一个计算器程序,模拟在计算器的EDIT框内产生字符,并模拟了5+7=12的全部按键过程
新建一个窗体,添加一个command和一个text框并粘贴以下代码
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetDlgItem Lib "user32" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Const WM_SETTEXT = &HC
Private Const WM_KEYUP = &H101
Private Const WM_KEYDOWN = &H100
Private Const SW_SHOWNORMAL = 1
Private Sub Command1_Click()
Static i As Integer
Dim hwn As Long
Dim twn As Long
If i = 0 Then "运行计算器
Text1.Text = "运行计算器"
hwn = FindWindow(vbNullString, "计算器") "查找窗体名称为"计算器"的句柄
If hwn = 0 Then
Shell "calc.exe"
Else
ShowWindow hwn, SW_SHOWNORMAL "激活窗口
SetForegroundWindow hwn
End If
End If
If i = 1 Then "向计算器文本框发送文本"5ZqweSC"
Text1.Text = "向计算器文本框发送文本"5ZqweSC""
hwn = FindWindow(vbNullString, "计算器")
twn = GetDlgItem(hwn, 403)
SendMessage twn, WM_SETTEXT, 0, "5ZqweSC"
End If
If i = 2 Then "计算器模拟按键Esc
Text1.Text = "计算器模拟按键Esc"
hwn = FindWindow(vbNullString, "计算器")
PostMessage hwn, WM_KEYDOWN, vbKeyEscape, 0
End If
If i = 3 Then "计算器模拟按键5
Text1.Text = "计算器模拟按键5"
hwn = FindWindow(vbNullString, "计算器")
PostMessage hwn, WM_KEYDOWN, vbKey5, 0
End If
If i = 4 Then "计算器模拟按键+
Text1.Text = "计算器模拟按键+"
hwn = FindWindow(vbNullString, "计算器")
PostMessage hwn, WM_KEYDOWN, vbKeyAdd, 0
End If
If i = 5 Then "计算器模拟按键7
Text1.Text = "计算器模拟按键7"
hwn = FindWindow(vbNullString, "计算器")
PostMessage hwn, WM_KEYDOWN, vbKey7, 0
End If
If i = 6 Then "计算器模拟按键Enter
Text1.Text = "计算器模拟按键回车"
hwn = FindWindow(vbNullString, "计算器")
PostMessage hwn, WM_KEYDOWN, vbKeyReturn, 0
End If
i = i + 1
If i = 7 Then i = 0
End Sub
- volcanoVol
-
SendKeys
语句
[编辑本段]描述
将一个或多个按键消息发送到活动窗口,就如同用键盘进行输入一样。
[编辑本段]语法
SendKeys
string[,
wait]
参数意义:
string部分必需的,字符串表达式,指定要发送的按键消息。
Wait可选的,指定等待方式的值。如果为
False(缺省值),则控件在按键发送出去之后立刻返回到过程;如果为
True,则按键消息必须在控件返回到过程之前加以处理。说明每个按键由一个或多个字符表示。
[编辑本段]用法
1)为了指定单一键盘字符,必须按字符本身的键。例如,为了表示字母
A,可以用
"A"
作为
string。为了表示多个字符,就必须在字符后面直接加上另一个字符。例如,要表示
A、B
及
C,可用
"ABC"
作为
string。
2)对
SendKeys
来说,加号+
、插入符^
、百分号%
、上划线~
及圆括号(
)
都具有特殊意义。为了指定上述任何一个字符,要将它放在大括号{}当中。例如,要指定正号,可用
{+}
表示。为了指定大括号字符,请使用
{{}
及
{}}。方括号
[
]
对
SendKeys
来说并不具有特殊意义,但必须将它们放在大括号中。在其它应用程序中,方括号有特殊意义,在出现动态数据交换
(DDE)
的时候,它可能具有重要意义。
3)为了在按下按键时指定那些不显示的字符,例如
ENTER
或
TAB
以及那些表示动作而非字符的按键.
例子:
set
WshShell
=
WScript.CreateObject("WScript.Shell")
"调用
WScript.Sleep
100
"延时0.1秒
WshShell.SendKeys
"ABCD"
"顺序发送ABCD四个键
- 天线宝宝说害怕
-
Option Explicit
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Sub Command1_Click()
Dim jsb As Long
jsb = FindWindow("notepad", vbNullString)
Dim mhwnd As Long
mhwnd = FindWindowEx(jsb, 0, "edit", vbNullString)
Dim lParam As Long
lParam = makelparam(vbKey5, False)
PostMessage mhwnd, WM_KEYDOWN, vbKey5, lParam
lParam = makelparam(vbKey5, True)
PostMessage mhwnd, WM_KEYUP, vbKey5, lParam
End Sub
Private Function makelparam(ByVal VirtualKey As Long, ByVal flag As Boolean) As Long
Dim s As String
Dim Firstbyte As String "lparam参数的24-31位
If flag = False Then "keydown
Firstbyte = "00"
Else
Firstbyte = "C0" "keyup
End If
Dim Scancode As Long
"获得虚拟键扫描码
Scancode = MapVirtualKey(VirtualKey, 0)
Dim Secondbyte As String "lparam参数的16-23位,即虚拟键扫描码
Secondbyte = Right("00" & Hex(Scancode), 2)
s = Firstbyte & Secondbyte & "0001" "0001为lparam参数的0-15位,即发送次数
makelparam = Val("&H" & s)
End Function
- 小菜G的建站之路
-
这个问题很简单,主要是通过sendmessage这个API来操作
代码如下
Option Explicit
" 查找运行的窗体
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
" 发送系统消息
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_CLOSE = &H10
Private Const WM_SYSCOMMAND = &H112
Private Const SC_CLOSE = &HF060&
Private Const SC_MINIMIZE = &HF020&
Private Const cch = 255
Private Sub Command1_Click()
Dim hwnd As Long
hwnd = FindWindow(vbNullString, "空白页 - Microsoft Internet Explorer")
"要想关闭或最小化别人的窗口,首先需要得到他的句柄,上面就是我通过标题来获得句人柄的方法
"SendMessage(hwnd, WM_CLOSE, 0, 0) "关闭窗体命令
SendMessage hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0 "最小化该窗口
End Sub
"