解决在CAPSLOCK开启情况下sendkeys大小写异常的问题

http://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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值