By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,551 Members | 2,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,551 IT Pros & Developers. It's quick & easy.

SendKeys turns off NumLock

twinnyfo
Expert Mod 2.5K+
P: 3,284
OK Race fans, here's a whacky one:

In a separate module, I have these declarations:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public Declare Function GetTickCount Lib "kernel32" () As Long
  5. Public Declare Function GetLastInputInfo Lib "user32" (pLII As Any) As Long
  6.  
  7. Private Type LastInputInformation
  8.     cbSize As Long
  9.     dwTime As Long
  10. End Type
  11.  
  12. Public Function GetUsersIdleTime() As Long
  13.     Dim LII As LastInputInformation
  14.     LII.cbSize = Len(LII)
  15.     Call GetLastInputInfo(LII)
  16.     GetUsersIdleTime = FormatNumber((GetTickCount() - LII.dwTime) / 1000, 2)
  17. End Function
  18.  
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:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Timer()
  2. On Error GoTo EH
  3.     If GetUsersIdleTime() >= 59 Then
  4.         SendKeys "^"
  5.     End If
  6.     Exit Sub
  7. EH:
  8.     MsgBox "There was an error with the Timer!  Please contact your Database Administrator.", vbCritical, "Error!"
  9.     Exit Sub
  10. End Sub
  11.  
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!
Oct 4 '12 #1

✓ answered by neelsfer

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
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private Declare Sub keybd_event Lib "user32" ( _
  4. ByVal bVk As Byte, _
  5. ByVal bScan As Byte, _
  6. ByVal dwFlags As Long, _
  7. ByVal dwExtraInfo As Long)
  8. Private Const VK_NUMLOCK = &H90
  9. Private Const KEYEVENTF_KEYUP = &H2
  10. Declare Function GetKeyState Lib "user32.dll" ( _
  11. ByVal nVirtKey As Long) As Integer
  12.  
  13. Sub numon()
  14.      'NUM_Off
  15.     NUM_On
  16. End Sub
  17.  
  18. Sub NUM_TOGGLE()
  19.      'Toggle NUM-Lock key state
  20.     keybd_event VK_NUMLOCK, 1, 0, 0
  21.     keybd_event VK_NUMLOCK, 1, KEYEVENTF_KEYUP, 0
  22. End Sub
  23.  
  24. Sub NUM_On() 'Turn NUM-Lock on
  25.     If Not (GetKeyState(vbKeyNumlock) = 1) Then
  26.         keybd_event VK_NUMLOCK, 1, 0, 0
  27.         keybd_event VK_NUMLOCK, 1, KEYEVENTF_KEYUP, 0
  28.     End If
  29. End Sub
  30.  
  31. Sub NUM_Off() 'Turn NUM-Lock off
  32.     If (GetKeyState(vbKeyNumlock) = 1) Then
  33.         keybd_event VK_NUMLOCK, 1, 0, 0
  34.         keybd_event VK_NUMLOCK, 1, KEYEVENTF_KEYUP, 0
  35.     End If
  36.  
  37. End Sub

Share this Question
Share on Google+
4 Replies


100+
P: 547
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
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private Declare Sub keybd_event Lib "user32" ( _
  4. ByVal bVk As Byte, _
  5. ByVal bScan As Byte, _
  6. ByVal dwFlags As Long, _
  7. ByVal dwExtraInfo As Long)
  8. Private Const VK_NUMLOCK = &H90
  9. Private Const KEYEVENTF_KEYUP = &H2
  10. Declare Function GetKeyState Lib "user32.dll" ( _
  11. ByVal nVirtKey As Long) As Integer
  12.  
  13. Sub numon()
  14.      'NUM_Off
  15.     NUM_On
  16. End Sub
  17.  
  18. Sub NUM_TOGGLE()
  19.      'Toggle NUM-Lock key state
  20.     keybd_event VK_NUMLOCK, 1, 0, 0
  21.     keybd_event VK_NUMLOCK, 1, KEYEVENTF_KEYUP, 0
  22. End Sub
  23.  
  24. Sub NUM_On() 'Turn NUM-Lock on
  25.     If Not (GetKeyState(vbKeyNumlock) = 1) Then
  26.         keybd_event VK_NUMLOCK, 1, 0, 0
  27.         keybd_event VK_NUMLOCK, 1, KEYEVENTF_KEYUP, 0
  28.     End If
  29. End Sub
  30.  
  31. Sub NUM_Off() 'Turn NUM-Lock off
  32.     If (GetKeyState(vbKeyNumlock) = 1) Then
  33.         keybd_event VK_NUMLOCK, 1, 0, 0
  34.         keybd_event VK_NUMLOCK, 1, KEYEVENTF_KEYUP, 0
  35.     End If
  36.  
  37. End Sub
Oct 4 '12 #2

twinnyfo
Expert Mod 2.5K+
P: 3,284
neels,

Thanks for the code.... I will give it a try and see how it works!
Oct 4 '12 #3

twinnyfo
Expert Mod 2.5K+
P: 3,284
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!
Oct 4 '12 #4

100+
P: 547
Glad i could help somebody, after having received so much help on this site!
Oct 4 '12 #5

Post your reply

Sign in to post your reply or Sign up for a free account.