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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

777 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