Solved

VB.Net: Reading NumLock status, even if the App doesn't have focus

Posted on 2011-09-26
2
331 Views
Last Modified: 2012-08-14
Hi,
I need to read the NumLock status in VB.net.
After some Googling I found the following Class:
Public Class KeyState

  Declare Function GetKeyboardState Lib "user32" Alias "GetKeyboardState" (ByVal pbKeyState() As Byte) As Long
  Private KeyCode As Integer

  Public Sub New(ByVal keycode As Integer)
    Me.KeyCode = keycode
  End Sub

  Public Function KeyState() As Boolean
    Dim state(256) As Byte
    GetKeyboardState(state)
    Return (iif(state(Me.KeyCode) = 1, True, False))
  End Function

End Class

This works fine if the App has focus, but does not read the key correctly if another App as focus.

Is there a way to do this?

Thanks,

Richard
0
Comment
Question by:rpm
2 Comments
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 500 total points
ID: 36598280
You can use GetKeyState http://msdn.microsoft.com/en-us/library/windows/desktop/ms646301(v=VS.85).aspx
Public Class Form1
    
    Private Declare Auto Function GetKeyState Lib "User32.dll" (ByVal nVirtKey As Integer) As Short

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '   keyBits(0)= ' low-bit
        '   keyBits(1)= ' high-bit
        '   If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. 
        '   The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard 
        '   will be on when the key is toggled, and off when the key is untoggled.

        Dim keyBits() As Byte = BitConverter.GetBytes(GetKeyState(Keys.NumLock))
        If keyBits(0) = 1 Then
            Debug.Print("Numlock ON")
        End If

    End Sub
End Class

Open in new window

0
 
LVL 1

Author Comment

by:rpm
ID: 36598372
Great, thanks! That does the trick!

Richard
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now