Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CapsLock Mouse over 1

Status
Not open for further replies.

assets

Technical User
Oct 23, 2002
574
AU
I am trying to find a way of using mouse over movement over a field to test if the CapsLock is on. This will be used on a password field where you do not see what has been typed in the field. I want top keep the case to be mixed.

Any help or suggestion would be appreciated.



Never give up never give in.

There are no short cuts to anything worth doing :)
 
You can test for caps lock (or pretty much any other key for that matter) using the GetKeyState API call. Just place the following in a code module somewhere or your form's class module, and then call CapsLockOn() in the onMouseOver event to determine if the Caps Lock key is on.

Code:
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

Public Function CapsLockOn() As Boolean
    Dim xState As Integer

    xState = GetKeyState(vbKeyCapital)
    CapsLockOn = (xState = 1 Or xState = -127)
End Function

The CapsLockOn function needs to be public if you place it in a code module.

Hope this helps,
Tom
 
Thanks Tom for your assistance,

I still having a problem. I made it public (placed in Module). When I call the code from OnActivate or OnMouseMove it does not work.

Never give up never give in.

There are no short cuts to anything worth doing :)
 
When I call the code from OnActivate or OnMouseMove it does not work
Any chance you could post how you call it ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PVH,

=CapsLockOn()

To run the code above

Never give up never give in.

There are no short cuts to anything worth doing :)
 
In the Enter (or GotFocus) event procedure of the password textbox you may add code similar to this:
If CapsLockOn Then
MsgBox "WARNING: CapsLock is on"
End If

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks for all the assistance. PVH still HAVING PROBLEMS THE TEXT BOX COMES UP BEFORE THE FOR HAS LOADED.

Never give up never give in.

There are no short cuts to anything worth doing :)
 
Another option is to create a Label on the form either right beside or below the password textbox. Have the label say something like "Warning: CapsLock is on" in red. Then in the OnGotFocus and OnChange procedures of the textbox add the code:

Me.lblWarn.visible = CapsLockOn()

This of course assumes that the label is named 'lblWarn'. You could even put this code in the forms's OnTimer event and set the time interval to something small to get an almost immediate change of the label as the key is pressed. Either of these might be considered overkill and might even bug the user if they actually use the caps lock key every time they change case instead of shift (which is very odd, but I have seen it more than once).

Tom
 
Thanks, Sorry for the delay Monday was a holiday. I Have missed something. When I click on the text box it said "Carn't finf Macro 'ME.' Macro does not exist"

This hapen when I use {Me.lblWarn.Visible = CapsLockOn()}

Any Ideas?

Thanks


Never give up never give in.

There are no short cuts to anything worth doing :)
 
Any Ideas?
Yes, all the code suggested above was VBA, no macro.
So change the OnXXXX properties to "event procedure" and click the ellipsis (...) on the right.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks again for all information. Sorry for delay I have been of work.

On Change of the password field has the following code.

On Error GoTo Err_Command0_Click

' Dim kb As New Keyboard Object
Dim kb As New Class1

If kb.Capslock = True Then
!Visible = CapsLockON.Visible
MsgBox "Caps Lock is on"

ElseIf kb.Capslock = False Then
MsgBox "caps Lock is off"

End If


Exit_Command0_Click:
Exit Sub

Err_Command0_Click:
MsgBox Err.Description
Resume Exit_Command0_Click

It works Except if you want caps in password each letter type brings up the message box. So I tried adding a label next to the password as Tom suggests with:
Me.lblWarn.visible = CapsLock() OR
Me.lblWarn.Visible = True

The code for Caps Lock etc is.

Option Compare Database

' From "VBA Developer's Handbook"
' by Ken Getz and Mike Gilbert
' Copyright 1997; Sybex, Inc. All rights reserved.

' Not all VBA implementations include this constant!
Private Const vbKeyScrollLock = 145

Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long
Property Get Capslock() As Boolean
' Return or set the Capslock toggle.
Capslock = CBool(GetKeyState(vbKeyCapital) And 1)
End Property

Property Get Numlock() As Boolean
' Return or set the Numlock toggle.
Numlock = CBool(GetKeyState(vbKeyNumlock) And 1)
End Property

Property Get ScrollLock() As Boolean
' Return or set the ScrollLock toggle.
' ScrollLock = CBool(GetKeyState(vbKeyScrollLock) And 1)
End Property

Property Let Capslock(Value As Boolean)
' Return or set the Capslock toggle.
Call SetKeyState(vbKeyCapital, Value)
End Property

Property Let Numlock(Value As Boolean)
' Return or set the Numlock toggle.
Call SetKeyState(vbKeyNumlock, Value)
End Property

Property Let ScrollLock(Value As Boolean)
' Return or set the ScrollLock toggle.
' Call SetKeyState(vbKeyScrollLock, Value)
End Property

Private Sub SetKeyState(intKey As Integer, fTurnOn As Boolean)
' Retrieve the keyboard state, set the particular
' key in which you're interested, and then set
' the entire keyboard state back the way it
' was, with the one key altered.
Dim abytBuffer(0 To 255) As Byte
GetKeyboardState abytBuffer(0)
abytBuffer(intKey) = CByte(Abs(fTurnOn))
SetKeyboardState abytBuffer(0)
End Sub


Any help eould be appreciated.


Never give up never give in.

There are no short cuts to anything worth doing :)
 
I'm sorry, but I can't seem to find in your post what exactly is the problem you are having. Here's what I get:
- You are running into a problem with the message box showing up every time the user types a letter if Caps Lock is on.
- You tried adding a label next to the password.

If the label idea is not working, try the following:
- Add a label and set its Name property to lblWarn
- Set the Label text to "Caps Lock is On!" or similar
- Put the following code in the OnChange event of the password field.
Code:
On Error GoTo HandleErrors
    Dim kb As Class1
 
    Set kb = New Class1
    Me![blue]lblWarn[/blue].Visible = kb.Capslock

ExitClean:
    Set kb = Nothing
    Exit Sub

HandleErrors:
    MsgBox Err.Description, vbInformation, Err.Number
    Resume ExitClean

If your label is called something other than lblWarn, replace the blue text above with the label name.

I also would suggest putting this code inside the form's OnLoad or OnOpen event so that the label is displayed immediately if Caps Lock is on.


Hope this helps,
Tom
 
A slight variation to Tom's solution, use a Timer on the form, this way you can show or hide the label as the Caps Lock key is toggled.
 
Tom, THANK YOU! THANK YOU" it now works fine.

Thank you earthandfire the timer idea is a good idea.

That why I like tek-tips other user come up with good idea.

Again thanks to ALL who have responded.

Never give up never give in.

There are no short cuts to anything worth doing :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top