' 定义Ctrl键,必须定义改键值,否则Ctrl键不能使用。
Public Const VK_CONTROL = &H11
' 定义KeyUp键
Public Const KEYEVENTF_KEYUP = &H2
' 参数bVk表示要模拟的按键的虚拟码,
' bScan表示该按键的扫描码(一般可以传0),
' dwFlags表示是按下键还是释放键(按下键为0,释放键为2),
' dwExtraInfo是扩展标志,一般没有用
Public Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
'根据指定的映射类型,执行不同的扫描码和字符转换,0—— wCode是个虚拟键码。函数返回相应的扫描码
Public Declare Function MapVirtualKey Lib "user32" _
Alias "MapVirtualKeyA" _
(ByVal wCode As Long, _
ByVal wMapType As Long) _
As Long
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
模拟Ctrl+C:
' 清空剪贴板
Clipboard.Clear
' 模拟Ctrl+C
keybd_event VK_CONTROL, 0, 0, 0 '按下Ctrl键
keybd_event vbKeyC, 0, 0, 0 'Asc("C"), 0, 0, 0 '按下C键
Sleep 500 '延时500毫秒
keybd_event vbKeyC, 0, KEYEVENTF_KEYUP, 0 '释放C键
keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0 '释放Ctrl键
Sleep 500 '需要一个延时毫秒
模拟Ctrl+V:
' 模拟Ctrl+V
keybd_event VK_CONTROL, 0, 0, 0 '按下Ctrl键
keybd_event vbKeyV, 0, 0, 0 '按下V键
Sleep 500 '延时500毫秒
keybd_event vbKeyV, 0, KEYEVENTF_KEYUP, 0 '释放V键
keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0 '释放Ctrl键
Sleep 100 '需要一个延时500毫秒