Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


status of caps lock and num lock

Posted on 2003-02-25
Medium Priority
Last Modified: 2011-09-20
how can i get the status of the num lock and caps lock keys from a vba function and hence include them on a form as either a label or a textbox.

Question by:russelldav
  • 2
  • 2
LVL 58

Accepted Solution

Jim Dettman (Microsoft MVP/ EE MVE) earned 200 total points
ID: 8019219
Option Compare Database
Option Explicit

Const ModuleName = "KeyboardFunctions"

'Key Codes from constant.txt in c:\access directory.

Global Const KEY_LBUTTON = &H1
Global Const KEY_RBUTTON = &H2
Global Const KEY_CANCEL = &H3
Global Const KEY_MBUTTON = &H4    ' NOT contiguous with L & RBUTTON
Global Const KEY_BACK = &H8
Global Const KEY_TAB = &H9
Global Const KEY_CLEAR = &HC
Global Const KEY_RETURN = &HD
Global Const KEY_SHIFT = &H10
Global Const KEY_CONTROL = &H11
Global Const KEY_MENU = &H12
Global Const KEY_PAUSE = &H13
Global Const KEY_CAPITAL = &H14
Global Const KEY_ESCAPE = &H1B
Global Const KEY_SPACE = &H20
Global Const KEY_PRIOR = &H21
Global Const KEY_NEXT = &H22
Global Const KEY_END = &H23
Global Const KEY_HOME = &H24
Global Const KEY_LEFT = &H25
Global Const KEY_UP = &H26
Global Const KEY_RIGHT = &H27
Global Const KEY_DOWN = &H28
Global Const KEY_SELECT = &H29
Global Const KEY_PRINT = &H2A
Global Const KEY_EXECUTE = &H2B
Global Const KEY_SNAPSHOT = &H2C
Global Const KEY_INSERT = &H2D
Global Const KEY_DELETE = &H2E
Global Const KEY_HELP = &H2F

' KEY_A thru KEY_Z are the same as their ASCII equivalents: 'A' thru 'Z'
' KEY_0 thru KEY_9 are the same as their ASCII equivalents: '0' thru '9'

Global Const KEY_NUMPAD0 = &H60
Global Const KEY_NUMPAD1 = &H61
Global Const KEY_NUMPAD2 = &H62
Global Const KEY_NUMPAD3 = &H63
Global Const KEY_NUMPAD4 = &H64
Global Const KEY_NUMPAD5 = &H65
Global Const KEY_NUMPAD6 = &H66
Global Const KEY_NUMPAD7 = &H67
Global Const KEY_NUMPAD8 = &H68
Global Const KEY_NUMPAD9 = &H69
Global Const KEY_MULTIPLY = &H6A
Global Const KEY_ADD = &H6B
Global Const KEY_SEPARATOR = &H6C
Global Const KEY_SUBTRACT = &H6D
Global Const KEY_DECIMAL = &H6E
Global Const KEY_DIVIDE = &H6F
Global Const KEY_F1 = &H70
Global Const KEY_F2 = &H71
Global Const KEY_F3 = &H72
Global Const KEY_F4 = &H73
Global Const KEY_F5 = &H74
Global Const KEY_F6 = &H75
Global Const KEY_F7 = &H76
Global Const KEY_F8 = &H77
Global Const KEY_F9 = &H78
Global Const KEY_F10 = &H79
Global Const KEY_F11 = &H7A
Global Const KEY_F12 = &H7B
Global Const KEY_F13 = &H7C
Global Const KEY_F14 = &H7D
Global Const KEY_F15 = &H7E
Global Const KEY_F16 = &H7F

Global Const KEY_NUMLOCK = &H90

Global Const KEY_GREYMINUS = 45
Global Const KEY_GREYPLUS = 43

Declare Function saGetKeyState Lib "user32" Alias "GetKeyState" (ByVal intVirtKey As Integer) As Integer
Declare Sub saGetKeyboardState Lib "user32" Alias "GetKeyBoardState" (ByVal strKeyState As String)
Declare Sub saSetKeyboardState Lib "user32" Alias "SetKeyboardState" (ByVal strKeyState As String)

Function IsCapsLockSet() As Integer
    IsCapsLockSet = IIf(saGetKeyState(KEY_CAPITAL), True, False)
End Function

Function IsNumLockSet() As Integer
    IsNumLockSet = IIf(saGetKeyState(KEY_NUMLOCK), True, False)
End Function

Sub SetCapsLock(fTurnOn As Integer)
    SetKeyState KEY_CAPITAL, fTurnOn
End Sub

Sub SetKeyState(intKey As Integer, fTurnOn As Integer)
    Dim strBuffer As String * 256

    saGetKeyboardState strBuffer
    Mid(strBuffer, intKey + 1, 1) = Chr$(IIf(fTurnOn, 1, 0))
    saSetKeyboardState strBuffer
End Sub

Sub SetNumLock(fTurnOn As Integer)
    SetKeyState KEY_NUMLOCK, fTurnOn
End Sub


Author Comment

ID: 8020621
great little function,

thanks there.

my only problem now is that obviously if the key state changes you have to re-run that function to get the change noticed.

is there any way of putting an ontimer event that will re-draw my form. i can then use this to check the status of the keys and also update the time (currently =Time() ).

LVL 58
ID: 8025276
<<is there any way of putting an ontimer event that will re-draw my form. i can then use this to check the status of the keys and also update the time (currently =Time() ).

 Sure.  ou can use OnTimer to do all that.  I would not set the interval for much less then a second or two though.


Author Comment

ID: 8025902

a second is jsut about ok (havnt tested with all functionality yet) but anything shorter and the display starts to go craszy.

thanks again

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Beware when using the ListIndex and the Column() properties of a listbox in Access 2007.  A bug has been identified in the Access 2007 listbox code which can cause the .ListIndex property to return a -1, and the .Columns(#) property to return a NULL…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Suggested Courses

575 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question