Question Whats wrong with this caps lock on/off code?

Haxaro

Well-known member
Joined
Mar 13, 2009
Messages
105
Programming Experience
1-3
I am trying to develop a code for myself, so a popup on my screen notifies me if my capslock button has been accidently pressed (because i dont have any way of knowing), which can get kind of anoying. But my code isnt exactly doing what i want:
VB.NET:
If Control.IsKeyLocked(Keys.CapsLock) = True Then
            Now = "on"
            If Now = Origional Then
                Exit Sub
            ElseIf Now <> Origional Then
                MsgBox("Caps has been turned on!")
                Origional = Now
            End If
        ElseIf Control.IsKeyLocked(Keys.CapsLock) = False Then
            Now = "off"
            If Now = Origional Then
                Exit Sub
            ElseIf Now <> Origional Then
                MsgBox("Caps has been turned off!")
                Origional = Now
            End If
        End If

Now to me, that code looks fine, but it only tells me if caps lock is on... any help please?
 
Code works for me. Here's the exact code I used :-

VB.NET:
    Private Now As String = ""
    Private Origional As String = ""

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If Control.IsKeyLocked(Keys.CapsLock) = True Then
            Now = "on"
            If Now = Origional Then
                Exit Sub
            ElseIf Now <> Origional Then
                MsgBox("Caps has been turned on!")
                Origional = Now
            End If
        ElseIf Control.IsKeyLocked(Keys.CapsLock) = False Then
            Now = "off"
            If Now = Origional Then
                Exit Sub
            ElseIf Now <> Origional Then
                MsgBox("Caps has been turned off!")
                Origional = Now
            End If
        End If
    End Sub
 
First, "Now" is a reserved keyword in VB and I would suggest renaming your variable to something different. Second is there really any need to check if the value changed? The Caps lock can only be on or off, so if it was pressed its value has changed.

VB.NET:
[COLOR="Blue"]Private Sub [/COLOR]Form1_KeyDown(...) [COLOR="blue"]Handles Me[/COLOR].KeyDown

    [COLOR="blue"]If [/COLOR]e.KeyCode <> Keys.CapsLock [COLOR="blue"]Then Exit Sub[/COLOR]
    MessageBox.Show([COLOR="blue"]String[/COLOR].Format([COLOR="DarkRed"]"Caps Lock is {0}", [/COLOR]_
                                  [COLOR="blue"]If[/COLOR](IsKeyLocked(Keys.CapsLock), [COLOR="darkred"]"On"[/COLOR], [COLOR="darkred"]"Off"[/COLOR])))

[COLOR="blue"]End Sub[/COLOR]
 
Thanks everyone. i got it working.

@Tom, That is true, but at the time, i didnt no if it was turned on or off, so i just put the variables in there.

Thanks again everyone :)
 
First, "Now" is a reserved keyword in VB and I would suggest renaming your variable to something different.
That is not true, Now is a readonly property in the VB runtime library. There is no problem naming a variable that, but it should have been 'now' regardless. A bigger problem is not posting the declaration and asking "what's wrong?", which of course leads anyone to believe this has something to do with date-time or implicit conversion problems. I'm one of those that like better to use the .Net library than the VB Runtimes so if Now was in question I'd choose Date.Now instead.

Perhaps you can use a StatusStrip to show the CapsLock status instead of the message box also?
 
Back
Top