sub sendKeysChar (curHwnd as long) Dim s As String s = "你要输入的文字" & vbCrLf & " 回车换行之后" Dim l As Long: l = 0 SendMessage1 curHwnd, WM_SETTEXT, 0, ByVal s SendMessage1 curHwnd, WM_CHAR, CLng(Asc("我")), CLng(0)' SendMessage1 curHwnd, WM_CHAR, 13, CLng(0)' SendMessage1 curHwnd, WM_CHAR, CLng(Asc("们")), CLng(0) SendMessage1 curHwnd, WM_CHAR, 9, CLng(0)' SendMessage1 curHwnd, WM_CHAR, CLng(Asc("我")), CLng(0) Dim vk_code As Long vk_code = VK_B Call PostMessage(curHwnd, WM_KEYDOWN, vk_code, ByVal MakeKeyLparam(vk_code, WM_KEYDOWN)) '按下A键 Sleep (20) Call PostMessage(curHwnd, WM_KEYUP, vk_code, ByVal MakeKeyLparam(vk_code, WM_KEYUP)) '释放A键 Sleep (20) vk_code = Asc("中") Call PostMessage(curHwnd, WM_CHAR, vk_code, ByVal 0&) '输入字符A Sleep (20) End Sub
Public Function MakeKeyLparam(ByVal VirtualKey As Long, ByVal flag As Long) As Long
Dim s As String
Dim Byte1 As String 'lparam参数的24-31位
Dim Byte2 As String 'lparam参数的16-23位,即虚拟键扫描码
If flag = WM_KEYDOWN Then '如果是按下键
Byte1 = "00"
Else
Byte1 = "C0" '如果是释放键
End If
'获得键的扫描码
Dim Scancode As Long: Scancode = MapVirtualKey(VirtualKey, 0)
Byte2 = Right("00" & Hex(Scancode), 2)
s = Byte1 & Byte2 & "0001" '0001为lparam参数的0-15位,即发送次数和其它扩展信息
MakeKeyLparam = Val("&H" & s)
Debug.Print Right("00000000" & CStr(Hex(MakeKeyLparam)), 8)
End Function
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Public Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
Public Const WM_SETHOTKEY = &H32
Public Const HOTKEYF_SHIFT = &H1 ' shift键
Public Const HOTKEYF_CONTROL = &H2 'ctrl键
Public Const HOTKEYF_ALT = &H4 'alt键
Public Const HOTKEYF_EXT = &H8 '附加键
Public Const WM_KEYFIRST = &H100
Public Const WM_CHAR = &H102
Public Const WM_DEADCHAR = &H103
Public Const WM_SYSKEYDOWN = &H104
Public Const WM_SYSKEYUP = &H105
Public Const WM_SYSCHAR = &H106
Public Const WM_SYSDEADCHAR = &H107
Public Const WM_KEYLAST = &H108
Public Const WM_INITDIALOG = &H110
Public Const WM_COMMAND = &H111
Public Const WM_SYSCOMMAND = &H112
Public Const WM_TIMER = &H113
Public Const WM_HSCROLL = &H114
Public Const WM_VSCROLL = &H115
Public Const WM_INITMENU = &H116
Public Const WM_INITMENUPOPUP = &H117
Public Const WM_MENUSELECT = &H11F
Public Const WM_MENUCHAR = &H120
Public Const WM_ENTERIDLE = &H121
Public Const BM_CLICK = &HF5 '单击
'▼键码常量表
Private Const KEYEVENTF_KEYUP = &H2
Public Const VK_LBUTTON = &H1
Public Const VK_RBUTTON = &H2
Public Const VK_CANCEL = &H3
Public Const VK_MBUTTON = &H4
Public Const VK_BACK = &H8
Public Const VK_TAB = &H9
Public Const VK_CLEAR = &HC
Public Const VK_RETURN = &HD
Public Const VK_SHIFT = &H10
Public Const VK_CONTROL = &H11
Public Const VK_MENU = &H12
Public Const VK_PAUSE = &H13
Public Const VK_CAPITAL = &H14
Public Const VK_ESCAPE = &H1B
Public Const VK_SPACE = &H20
Public Const VK_PRIOR = &H21
Public Const VK_NEXT = &H22
Public Const VK_END = &H23
Public Const VK_HOME = &H24
Public Const VK_LEFT = &H25
Public Const VK_UP = &H26
Public Const VK_RIGHT = &H27
Public Const VK_DOWN = &H28
Public Const VK_Select = &H29
Public Const VK_PRINT = &H2A
Public Const VK_EXECUTE = &H2B
Public Const VK_SNAPSHOT = &H2C
Public Const VK_Insert = &H2D
Public Const VK_Delete = &H2E
Public Const VK_HELP = &H2F
Public Const VK_0 = &H30
Public Const VK_1 = &H31
Public Const VK_2 = &H32
Public Const VK_3 = &H33
Public Const VK_4 = &H34
Public Const VK_5 = &H35
Public Const VK_6 = &H36
Public Const VK_7 = &H37
Public Const VK_8 = &H38
Public Const VK_9 = &H39
Public Const VK_A = &H41
Public Const VK_B = &H42
Public Const VK_C = &H43
Public Const VK_D = &H44
Public Const VK_E = &H45
Public Const VK_F = &H46
Public Const VK_G = &H47
Public Const VK_H = &H48
Public Const VK_I = &H49
Public Const VK_J = &H4A
Public Const VK_K = &H4B
Public Const VK_L = &H4C
Public Const VK_M = &H4D
Public Const VK_N = &H4E
Public Const VK_O = &H4F
Public Const VK_P = &H50
Public Const VK_Q = &H51
Public Const VK_R = &H52
Public Const VK_S = &H53
Public Const VK_T = &H54
Public Const VK_U = &H55
Public Const VK_V = &H56
Public Const VK_W = &H57
Public Const VK_X = &H58
Public Const VK_Y = &H59
Public Const VK_Z = &H5A
Public Const VK_STARTKEY = &H5B
Public Const VK_CONTEXTKEY = &H5D
Public Const VK_NUMPAD0 = &H60
Public Const VK_NUMPAD1 = &H61
Public Const VK_NUMPAD2 = &H62
Public Const VK_NUMPAD3 = &H63
Public Const VK_NUMPAD4 = &H64
Public Const VK_NUMPAD5 = &H65
Public Const VK_NUMPAD6 = &H66
Public Const VK_NUMPAD7 = &H67
Public Const VK_NUMPAD8 = &H68
Public Const VK_NUMPAD9 = &H69
Public Const VK_MULTIPLY = &H6A
Public Const VK_ADD = &H6B
Public Const VK_SEPARATOR = &H6C
Public Const VK_SUBTRACT = &H6D
Public Const VK_DECIMAL = &H6E
Public Const VK_DIVIDE = &H6F
Public Const VK_F1 = &H70
Public Const VK_F2 = &H71
Public Const VK_F3 = &H72
Public Const VK_F4 = &H73
Public Const VK_F5 = &H74
Public Const VK_F6 = &H75
Public Const VK_F7 = &H76
Public Const VK_F8 = &H77
Public Const VK_F9 = &H78
Public Const VK_F10 = &H79
Public Const VK_F11 = &H7A
Public Const VK_F12 = &H7B
Public Const VK_F13 = &H7C
Public Const VK_F14 = &H7D
Public Const VK_F15 = &H7E
Public Const VK_F16 = &H7F
Public Const VK_F17 = &H80
Public Const VK_F18 = &H81
Public Const VK_F19 = &H82
Public Const VK_F20 = &H83
Public Const VK_F21 = &H84
Public Const VK_F22 = &H85
Public Const VK_F23 = &H86
Public Const VK_F24 = &H87
Public Const VK_NUMLOCK = &H90
Public Const VK_OEM_SCROLL = &H91
Public Const VK_OEM_1 = &HBA
Public Const VK_OEM_PLUS = &HBB
Public Const VK_OEM_COMMA = &HBC
Public Const VK_OEM_MINUS = &HBD
Public Const VK_OEM_PERIOD = &HBE
Public Const VK_OEM_2 = &HBF
Public Const VK_OEM_3 = &HC0
Public Const VK_OEM_4 = &HDB
Public Const VK_OEM_5 = &HDC
Public Const VK_OEM_6 = &HDD
Public Const VK_OEM_7 = &HDE
Public Const VK_OEM_8 = &HDF
Public Const VK_ICO_F17 = &HE0
Public Const VK_ICO_F18 = &HE1
Public Const VK_OEM102 = &HE2
Public Const VK_ICO_HELP = &HE3
Public Const VK_ICO_00 = &HE4
Public Const VK_ICO_CLEAR = &HE6
Public Const VK_OEM_RESET = &HE9
Public Const VK_OEM_JUMP = &HEA
Public Const VK_OEM_PA1 = &HEB
Public Const VK_OEM_PA2 = &HEC
Public Const WM_KEYDOWN = &H100 '按下键盘按键
Public Const WM_KEYUP = &H101 '弹起键盘按键
Public Const WM_LBUTTONDOWN = &H201 '鼠标左键按下
Public Const WM_LBUTTONUP = &H202 '鼠标左键弹起
Public Const WM_LBUTTONDBLCLK = &H203 '鼠标左键双击
Public Const WM_RBUTTONDOWN = &H204 '鼠标右键按下
Public Const WM_RBUTTONUP = &H205 '鼠标右键弹起
Public Const WM_RBUTTONDBLCLK = &H206 '鼠标右键双击
Public Const WM_MBUTTONDBLCLK = &H209 '鼠标中间键双击
Public Const WM_MBUTTONDOWN = &H207 '鼠标中间键按下
Public Const WM_MBUTTONUP = &H208 '鼠标中间键弹起
Public Const WM_MOUSEMOVE = &H200 '鼠标移动常数
'----自定义键盘状态常量-----
Public Const GAKB_LOCKED = &HFF80 '按键锁定状态。
Public Const GAKB_KEYDOWN = &H80 '按键按下状态。
Public Const strPerfact = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
不说废话直接上 能用的代码. 查了不少资料 . vba vb6 使用 API 有好多的坑
可以 自己写个类封装一下.
注意 一下 参数的引用,注意 虚拟键码的获取,注意按下谈起的不同.
注意 发送按键和发送字符的不同. 注意 系统按键 和 普通按键的不同.