https://files.cnblogs.com/files/liuzhaoyzz/sendkeys%E5%A4%A7%E5%B0%8F%E5%86%99.rar
首先利用GetKeyState(vbKeyCapital) 的API获取capslock的状态,如果开启了,就SendKeys "{capslock}",
然后SendKeys "s",这样子的语句,默认=SendKeys "s",false的,如果操作其他程序,最后一个sendkeys之后,
必须用doevents交还控制权。试试看下面的代码在CAPSLOCK开启和关闭状态下的情况。
所以最好还是要用SendKeys "s",false,会获得你想要的结果,这也是sendkeys的默认值。
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Sub Command1_Click()
If GetKeyState(vbKeyCapital) = 1 Then
SendKeys "{capslock}"
End If
Text1.SetFocus
SendKeys "s"
' SendKeys "{CAPSLOCK}" ' Turn on the CapsLock
SendKeys "o" ' will be printed in caps
For i = 0 To 10
SendKeys Chr(Asc("A") + i) 'will be printed in caps
Next
'DoEvents ' uncommenting this will print next line in smalls
SendKeys "o" ' will be printed in caps
' SendKeys "{CAPSLOCK}" ' Turn off the caps lock
SendKeys "o" ' Will be printed in smalls
End Sub
Private Sub Command2_Click()
If GetKeyState(vbKeyCapital) = 1 Then
SendKeys "{capslock}"
End If
Text1.SetFocus
SendKeys "s", True
' SendKeys "{CAPSLOCK}", True ' Turn on the CapsLock
SendKeys "o", True ' will be printed in caps
For i = 0 To 10
SendKeys Chr(Asc("A") + i), True 'will be printed in caps
Next
'DoEvents ' uncommenting this will print next line in smalls
SendKeys "o", True ' will be printed in caps
' SendKeys "{CAPSLOCK}", True ' Turn off the caps lock
SendKeys "o", True ' Will be printed in smalls
End Sub
Private Sub Command3_Click()
Text1 = ""
End Sub