Detect whether Caps Lock key pressed?

  • Thread starter Thread starter yxq
  • Start date Start date
Y

yxq

Hello,
I have a main form, there other some controls(i.e. textbox) in the form.
I want to detect whether Caps Lock key is pressed in form-key event.

Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
If e.KeyCode = 20 Then
MessageBox.Show("Caps Lock is pressed!")
End If
End Sub

But the code will not work, how to do? I guess the other controls accepted
the key?

Thank you
 
Hi,

Use the getkeystate api. It will return true if the key is
active.
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Integer) As
boolean

Const VK_NUMLOCK = 144

Const VK_CAPLOCK = 20



Ken

------------------
Hello,
I have a main form, there other some controls(i.e. textbox) in the form.
I want to detect whether Caps Lock key is pressed in form-key event.

Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
If e.KeyCode = 20 Then
MessageBox.Show("Caps Lock is pressed!")
End If
End Sub

But the code will not work, how to do? I guess the other controls accepted
the key?

Thank you
 
Hello,
I have a main form, there other some controls(i.e. textbox) in the form.
I want to detect whether Caps Lock key is pressed in form-key event.

Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
If e.KeyCode = 20 Then
MessageBox.Show("Caps Lock is pressed!")
End If
End Sub

Use Keys.CapsLock instead of 20 then no-one has to guess
what the 20 means.

But the code will not work, how to do? I guess the other controls accepted
the key?

That code will work. Have you set the form's KeyPreview =
true?



Jan Hyde (VB MVP)

--
I was the best I ever had.

Woody Allen

[Abolish the TV License - http://www.tvlicensing.biz/]
 
Back
Top