detect if NumLock is OFF and turn it always back O

2019-01-29 07:23发布

问题:

What is a method to detect if NumLock is OFF and how to turn it always back ON automatically when my VB APP is running ?

EDIT: My app is dedicated app, running on dedicated computer with external numpad device.

another option would be to ACCEPT NUMPAD ARROW KEYS (etc) and convert these to NUMBERS on fly, is it possible?? (e.g. ignore the numlock off situation and behave as numlock is on)

回答1:

I'm not sure how you do this specifically in vb.net (a quick google found http://support.microsoft.com/kb/177674), but generally speaking changing machine-wide settings like this is frowned upon: users are accustomed to their individual preference for numlock (or capslock -- or whatever) key state...and you're overriding it.

This would really irritate me.

That said, in circumstances where your app is the only thing running (e.g., POS software, medical office management software, etc) this might be ok.



回答2:

Physically remove NUMPAD key ?



回答3:

    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Private Const KEYEVENTF_KEYUP = &H2
    Const VK_NUMLOCK = &H90
    Const KEYEVENTF_EXTENDEDKEY = &H1
    Declare Function GetKeyState Lib "user32" Alias "GetKeyState" (ByVal ByValnVirtKey As Integer) As Short

 Private Sub numlockON()
        keybd_event(VK_NUMLOCK, 0, 0, 0) ' Press NUMLOCK key down
        keybd_event(VK_NUMLOCK, 0, KEYEVENTF_KEYUP, 0) ' Release it
    End Sub

If Not GetKeyState(VK_NUMLOCK) Then numlockON()


回答4:

Public Const VK_NUMLOCK = &H90
Declare Function GetKeyState Lib "user32" Alias "GetKeyState" _
(ByVal ByValnVirtKey As Integer) As Short

Private Sub me_keyDown(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = Keys.NumLock And Not NLKey Then
        If Not GetKeyState(VK_NUMLOCK) Then
            e.Handled = True
            NumlOn.Start()
            Exit Sub
        End If
    End If
End sub

Private Sub NumlOn_Tick(sender As Object, e As EventArgs) Handles NumlOn.Tick
    NLKey = True
    If Not GetKeyState(VK_NUMLOCK) Then numlockON()
    Application.DoEvents()
    NumlOn.Stop()
    NLKey = False
End Sub