samedi 28 mai 2016

Issue toggling caps lock with check box in vb .net

I've developed an application (with vb .net) that can toggle caps lock state by clicking on a check box. I've coded the program in such a way that when I click on the checkbox, if it gets checked the caps lock must be turned on and when unchecked it must turn off. Below are the codes.

Public Class Form1

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

Private Const VK_CAPITAL As Integer = &H14
Private Const VK_SCROLL As Integer = &H91
Private Const VK_NUMLOCK As Integer = &H90

Private Const KEYEVENTF_EXTENDEDKEY As Integer = &H1
Private Const KEYEVENTF_KEYUP As Integer = &H2

Private Sub checkbutton_caps_CheckedChanged(sender As Object, e As EventArgs) Handles checkbutton_caps.CheckStateChanged

    If checkbutton_caps.Checked = True Then
        Call keybd_event(VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0)
        checkbutton_caps.Image = Image.FromFile("resources\btn_ico_caps_on.png")

    ElseIf checkbutton_caps.Checked = False Then
        Call keybd_event(VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
        checkbutton_caps.Image = Image.FromFile("resources\btn_ico_caps_off.png")

    End If
End Sub
End Class

Now the problem is it's not working as expected. If I check the checkbox, only the image of the checkbox changes but not the caps lock status. The caps lock status changes only when I click on the check box twice. So I need to click on the check box twice to toggle the caps lock. I suspect there's a problem in the way I've used the conditional statements.




Aucun commentaire:

Enregistrer un commentaire