Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-09-26
2
Medium Priority
?
346 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

609 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