Solved

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

Posted on 2011-09-26
2
332 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

832 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