Ativando o Caps lock via código
1a. Parte:
Insira o seguinte código nas declarações gerais de um módulo .BAS
Public Const VK_CAPITAL = &H14
Public Type KeyboardBytes
kbByte(0 To 255) As Byte
End Type
Public kbArray As KeyboardBytes
Public Declare Function GetKeyState Lib “user32” (ByVal nVirtKey As
Long) As Long
Public Declare Function GetKeyboardState Lib “user32” (kbArray As
KeyboardBytes) As Long
Public Declare Function SetKeyboardState Lib “user32” (kbArray As
KeyboardBytes) As Long
2a. Parte
Em um form, coloque 3 command buttons (cmdToggle, cmdTurnOff, cmdTurnOff) e
uma label. Coloque o seguinte código no form:
Private Function CapsLock() As Integer
CapsLock = GetKeyState(VK_CAPITAL) And 1 = 1
End Function
Private Sub Form_Load()
If CapsLock() = 1 Then Label1 = “On” Else Label1 = “Off”
End Sub
Private Sub cmdToggle_Click()
GetKeyboardState kbArray
kbArray.kbByte(VK_CAPITAL) = IIf(kbArray.kbByte(VK_CAPITAL) = 1, 0, 1)
SetKeyboardState kbArray
Label1 = IIf(CapsLock() = 1, “On”, “Off”)
End Sub
Private Sub cmdTurnOn_Click()
GetKeyboardState kbArray
kbArray.kbByte(VK_CAPITAL) = 1
SetKeyboardState kbArray
Label1 = IIf(CapsLock() = 1, “On”, “Off”)
End Sub
Private Sub cmdTurnOff_Click()
GetKeyboardState kbArray
kbArray.kbByte(VK_CAPITAL) = 0
SetKeyboardState kbArray
Label1 = IIf(CapsLock() = 1, “On”, “Off”)
End Sub