473,900 Members | 4,979 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SendKeys turns off NumLock

3,653 Recognized Expert Moderator Specialist
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
  4. Public Declare Function GetTickCount Lib "kernel32" () As Long
  5. Public Declare Function GetLastInputInfo Lib "user32" (pLII As Any) As Long
  7. Private Type LastInputInformation
  8.     cbSize As Long
  9.     dwTime As Long
  10. End Type
  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
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
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.

Oct 4 '12 #1
4 16164
547 Contributor
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
  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
  13. Sub numon()
  14.      'NUM_Off
  15.     NUM_On
  16. End Sub
  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
  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
  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
  37. End Sub
Oct 4 '12 #2
3,653 Recognized Expert Moderator Specialist

Thanks for the code.... I will give it a try and see how it works!
Oct 4 '12 #3
3,653 Recognized Expert Moderator Specialist

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
547 Contributor
Glad i could help somebody, after having received so much help on this site!
Oct 4 '12 #5

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

Similar topics

by: RBohannon | last post by:
I need to create a report in MS Word populated with data from A2K. I have been asked to create the report in Word so that parts of it can be edited as necessary later. The data in the report are in a table with headings for each column, so converting an Access report to a Word doc in RTF doesn't work because only the text is converted, not the table. What I have tried so far, with some success, is to write the report directly in Word....
by: George | last post by:
Every time I used the Sendkeys command in my application the "Numlock" turned off and I couldn't use the keypad to hit numbers...... The old code was: Private Sub Command1_Click() SendKeys "{DOWN}" SendKeys "^{END}" SendKeys "{TAB}" End Sub
by: James Kirkup | last post by:
Hello, Does anyone know in code how to turn the NumLock key on in C#? with regards James *** Sent via Developersdex http://www.developersdex.com ***
by: Chris | last post by:
can anyone out there tell me how do u check the state of these keys in vb.net? or is there any components in the .NET Framework that supports doing so? any help is appreciated. Thanks.
by: Woody Splawn | last post by:
Could someone tell me what code I run to determine if the user's numlock is on? Is there code I can run to set the numlock to on?
by: Stan Sainte-Rose | last post by:
Hi I saw a post about numlock but I can't not read it. I would like to check the state of the numlock key and if it's not active, set its status on active. How can I accomplish that in vb ? Stan
by: Phil Galey | last post by:
I have an application written in VB.NET, running on Windows 2000 SP 3. It uses a process object and AppActivate, followed by SendKeys commands, to control a running QuarkXPress application. On the development workstation, it works fine; however on the target workstation, SendKeys fails. Both workstations are Windows 2000 SP3 and both have the same version of QuarkXPress running. I've found articles that talk about sending lower-case...
by: lgbjr | last post by:
Hi All, In VB.Net how do I determine if CapsLock is on/off (same for NumLock) and whether the input mode is set to Insert or Overwrite (Insert Key). I just added a status bar to my app, and I want to reflect the state of these three items (similar to Word, Excel, etc.) I've found the key enumerator value for each key, but I don't know how to tell if the state is on/off, etc. TIA Lee
by: Joe S | last post by:
I have been searching all over for how to use SendKeys to send Keys.RControlKey. I've tried all manner of byte conversions and just can't get it to work. Pity there is no {RCONTROL} defined for me! Any help is very much appreciated.
by: Z.K. | last post by:
I am trying to detect if the Numlock is pressed. I can do it okay with the CapsLock or the ScrLk, but not with NumLock. The code for all three checks is exactly the same, but for some reason numlock always thinks it is on. Z.K. code:
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.