如何判断caps lock(大小写)的状态

mi6236 2004-07-31 11:44:07
如text1得到焦点无论caps lock的状态是开或关都置为开
如text2得到焦点无论caps lock的状态是开或关都置为关
...全文
327 6 打赏 收藏 举报
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
mi6236 2004-08-01
谢谢两位高手的热心帮助
  • 打赏
  • 举报
回复
mi6236 2004-07-31
能给一段完整代码吗?
  • 打赏
  • 举报
回复
TechnoFantasy 2004-07-31
http://www.china-askpro.com/msg27/qa45.shtml
  • 打赏
  • 举报
回复
online 2004-07-31
需要调用GetKeyboardState()函数获得键盘状态。这两个函数都需要一个256个字节的数组作为参数。这个数组记录着键盘状态,每个键在数组中的位置由VK_常数决定
SetKeyboardState在NT下通常不能很好的工作.所以建议使用keybd_event


Option Explicit
Private Const VK_CAPITAL = &H14
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2

Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Declare Function GetKeyboardState Lib "user32" _
(pbKeyState As Byte) As Long
Private Declare Function SetKeyboardState Lib "user32" _
(lppbKeyState As Byte) As Long

Public Sub CapsLockOn()
Dim Key(255) As Byte
Dim bflag As Boolean

'得到键盘状态
GetKeyboardState Key(0)

bflag = Key(VK_CAPITAL)

If bflag Then
'每一次击键,都有key press(按下键)和key release(释放键)
keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
End If
End Sub

Public Sub CapsLockOff()
Dim Key(255) As Byte
Dim bflag As Boolean

'得到键盘状态
GetKeyboardState Key(0)

bflag = Key(VK_CAPITAL)

If Not bflag Then
keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
End If
End Sub

Private Sub Text2_GotFocus()
CapsLockOff
End Sub

Private Sub Text1_GotFocus()
CapsLockOn
End Sub
  • 打赏
  • 举报
回复
rainstormmaster 2004-07-31
//ES_UPPERCASE(自动转换为小写)
应为:
ES_UPPERCASE(自动转换为大写)、
  • 打赏
  • 举报
回复
rainstormmaster 2004-07-31
//如text1得到焦点无论caps lock的状态是开或关都置为开
如text2得到焦点无论caps lock的状态是开或关都置为关

这个问题不用设置键盘状态,其实就是让text1自动转换为大写,让text2自动转换为小写,分别给两个textbox附加ES_UPPERCASE(自动转换为小写)、ES_LOWERCASE (自动转换为小写)风格就可以了,当然,这只是针对你问题的特例:

Option Explicit
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Const GWL_EXSTYLE = (-20)
Private Const GWL_STYLE = (-16)
Private Const ES_LOWERCASE = &H10&
Private Const ES_UPPERCASE = &H8&

Private Sub Form_Load()
Dim style As Long
style = GetWindowLong(Text1.hwnd, GWL_STYLE)
style = style Or ES_UPPERCASE
SetWindowLong Text1.hwnd, GWL_STYLE, style

style = GetWindowLong(Text2.hwnd, GWL_STYLE)
style = style Or ES_LOWERCASE
SetWindowLong Text2.hwnd, GWL_STYLE, style
End Sub
  • 打赏
  • 举报
回复
相关推荐
发帖
API
加入

1471

社区成员

VB API
社区管理员
  • API
申请成为版主
帖子事件
创建了帖子
2004-07-31 11:44
社区公告
暂无公告