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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting Caps Lock 1

Status
Not open for further replies.

grnzbra

Programmer
Mar 12, 2002
1,273
US
Is there any way to determine, during the opening of a form, if the Caps Lock is on (and turn it off or lock the form)?
 
Yes.

Code:
Private Declare Sub GetKeyboardState Lib "user32" (lpKeyState As Any)
Const VK_CAPITAL = &H14

<...>
'Array to check capslock
GetKeyboardState KeyboardBuffer(0)
 
OK. Now I'm confused. I tried looking up the various terms in the code in Help and couldn't find anything. Could you explanin what's going on in the code?
 
Sorry, I should have added:

Code:
If KeyboardBuffer(VK_CAPITAL) And 1 Then 
'CapsLock on ...
    
End If
 
Sorry, I'm not paying attention :(

Code:
Sub CapsOn()
ReDim KeyboardBuffer(256) As Byte
'Array to check capslock
GetKeyboardState KeyboardBuffer(0)
If KeyboardBuffer(VK_CAPITAL) And 1 Then
'CapsLock on ...
  Debug.Print "Caps On"
End If
End Sub
 
What is going on in:

Private Declare Sub GetKeyboardState Lib "user32" (lpKeyState As Any)

?

I've never seen the word Declare between Private and Sub in any code before. Same for Lib "user32" between the sub name and the parameter. What kind of parameter is Any?
 
Thanks for the references. One of the things I got was that it must be set up in a Class module. (It all compiles very nicely when it's in the class module, but not when in a normal module. However, nothing in a normal module seems to be able to see into the class module; I keep getting errors saying that whatever it is that I am calling in the class module doesn't exist. I need to call this from the OnOpen event of a form.
 
Just use an ordinary module, not one attached to a form. Your code will look exactly like this:
Code:
Option Compare Database
Option Explicit

Private Declare Sub GetKeyboardState Lib "user32" (lpKeyState As Any)
Const VK_CAPITAL = &H14

Function CapsOn()
ReDim KeyboardBuffer(256) As Byte
'Array to check capslock
GetKeyboardState KeyboardBuffer(0)
If KeyboardBuffer(VK_CAPITAL) And 1 Then
'CapsLock on ...
   CapsOn = True
Else
    CapsOn = False
End If
End Function

You can now use the function CapsOn anywhere.
 
GOT IT!!! IT WORKS!!! THANK YOU VERY MUCH.
 
thanks for this code, it works perfect.
If caps is on, The message should be the same that displays in the win xp when a user is typing his/her id and password,but the questions is : Is there any problem if i make my customized software to display the same message that windos displays:
(because of the intelectual property)

"Caps Locks is on
Having Caps Lock on may cause you to enter your password incorrectly.
You should press Caps lock to turn it off before entering your password. "

Thanks
 
This is a quote from the Wiki article (above), but you may wish to check a little further:

[tt]Other companies propagate their APIs freely. For example, Microsoft deliberately makes most of its API information public, so that software will be written for the Windows platform. The sale of the third-party software sells copies of Microsoft Windows. This is typical of companies who profit from the sale of API implementations (in this case, Microsoft Windows, which is sold at a gain for Microsoft).[/tt]
 
thanks for your info Remou. i will check a bit more regarding this issue
 
Easy,

Sounds like you are asking about the "Caps Locks is on
Having Caps Lock on may cause you to enter your password incorrectly.
You should press Caps lock to turn it off before entering your password. " statement. It doesn't seem to be something that would be copywriteable (but you never know).

Personally, I find it offensive. If you really want to secure something, type in a passphrase with normal capitalization, but with the CapsLock on. I once worked at a job where all the files had the same password, "Book". Inadvertantly, when I set mine up, I did it with the CapsLock on and got "bOOK". Three weeks after I moved on to a new job, I got a call begging for the password; they had gone crazy for three weeks trying all sorts of variations on my name, names of family members etc, but no one ever thought of using the universal password for the office but with the caps lock on. I want to be able to use passphrases with the caps lock on. (I also mechanically scramble the letters)
 
Thanks grnzbra for your comments,

I need to use that "Capslock warning message" because, the password is case sensitive, it's like the password wou type when accesing Windows (XP, NT, ...etc) so it has to be case sensitive hence that i need to use that warning message.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top