SendKeys turns off NumLock

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • twinnyfo
    Recognized Expert Moderator Specialist
    • Nov 2011
    • 3653

    SendKeys turns off NumLock

    OK Race fans, here's a whacky one:

    In a separate module, I have these declarations:

    Code:
    Option Compare Database
    Option Explicit
    
    Public Declare Function GetTickCount Lib "kernel32" () As Long
    Public Declare Function GetLastInputInfo Lib "user32" (pLII As Any) As Long
     
    Private Type LastInputInformation
        cbSize As Long
        dwTime As Long
    End Type
     
    Public Function GetUsersIdleTime() As Long
        Dim LII As LastInputInformation
        LII.cbSize = Len(LII)
        Call GetLastInputInfo(LII)
        GetUsersIdleTime = FormatNumber((GetTickCount() - LII.dwTime) / 1000, 2)
    End Function
    These allow me to check whether someone has been using their machine and how long the system has been idle.

    In the Timer Event of a form, set to 60,000 (60 seconds), I have this code:

    Code:
    Private Sub Form_Timer()
    On Error GoTo EH
        If GetUsersIdleTime() >= 59 Then
            SendKeys "^"
        End If
        Exit Sub
    EH:
        MsgBox "There was an error with the Timer!  Please contact your Database Administrator.", vbCritical, "Error!"
        Exit Sub
    End Sub
    What this does, and it works rather well, is it checks to see if the user has been active on the computer within the last 59 seconds. If not, it "types" a keystroke, in this case, it simulates hitting and releasing the Ctrl-Key. This prevents our screen savers from activating, thus breaking our network connections with the database. Our users will often have to review documents while the DB sits idle. This saves a lot of headaches, and some of you may be familiar with some of my recent posts conerning this.

    Here is the issue: When I open this form, when the SendKeys statement activates, the NumLock status automatically turns OFF (it never turns itself back on, but must be turned on manually). This is a real pain, because this form uses the SSN of a person to search, so we often don't realize the NumLock is off until after we start keying in the number. (I have also simulated the Shift Key--"+" with similar results).

    This is not a huge deal, but it is really annoying. This is the only thing that has changed on our machines, and it MUST be the form, because when I've opened the form and just let it sit, the NumLock turns off at exactly 60 seconds! (although not all the time--even stranger!)

    Any thoughts or ideas on why this is happening and a way to prevent it from happening? Perhaps checking and resetting NumLock status--which I don't know how to do....

    I am open to any ideas.

    Thanks!
  • neelsfer
    Contributor
    • Oct 2010
    • 547

    #2
    Maybe this will help if i am not missing the plot here.. I have the following code in a module, and call it with numon
    Code:
    Option Compare Database
    
    Private Declare Sub keybd_event Lib "user32" ( _
    ByVal bVk As Byte, _
    ByVal bScan As Byte, _
    ByVal dwFlags As Long, _
    ByVal dwExtraInfo As Long)
    Private Const VK_NUMLOCK = &H90
    Private Const KEYEVENTF_KEYUP = &H2
    Declare Function GetKeyState Lib "user32.dll" ( _
    ByVal nVirtKey As Long) As Integer
     
    Sub numon()
         'NUM_Off
        NUM_On
    End Sub
     
    Sub NUM_TOGGLE()
         'Toggle NUM-Lock key state
        keybd_event VK_NUMLOCK, 1, 0, 0
        keybd_event VK_NUMLOCK, 1, KEYEVENTF_KEYUP, 0
    End Sub
     
    Sub NUM_On() 'Turn NUM-Lock on
        If Not (GetKeyState(vbKeyNumlock) = 1) Then
            keybd_event VK_NUMLOCK, 1, 0, 0
            keybd_event VK_NUMLOCK, 1, KEYEVENTF_KEYUP, 0
        End If
    End Sub
     
    Sub NUM_Off() 'Turn NUM-Lock off
        If (GetKeyState(vbKeyNumlock) = 1) Then
            keybd_event VK_NUMLOCK, 1, 0, 0
            keybd_event VK_NUMLOCK, 1, KEYEVENTF_KEYUP, 0
        End If
    
    End Sub

    Comment

    • twinnyfo
      Recognized Expert Moderator Specialist
      • Nov 2011
      • 3653

      #3
      neels,

      Thanks for the code.... I will give it a try and see how it works!

      Comment

      • twinnyfo
        Recognized Expert Moderator Specialist
        • Nov 2011
        • 3653

        #4
        Neels,

        Took a few very minor tweaks to add this code to a public module, then I reset the idle time, and use your code to make sure the NumLock is currently on. Success!

        Thanks much!

        Comment

        • neelsfer
          Contributor
          • Oct 2010
          • 547

          #5
          Glad i could help somebody, after having received so much help on this site!

          Comment

          Working...