?
Solved

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

Posted on 2011-09-26
2
Medium Priority
?
337 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

762 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