Solved

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

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

22 Experts available now in Live!

Get 1:1 Help Now