检测NumLock是否关闭并将其始终重新打开

时间:2009-06-02 06:24:33

标签: vb.net input keyboard

当我的VB APP运行时,检测NumLock是否关闭以及如何将其自动重新打开的方法是什么?

编辑: 我的应用程序是专用应用程序,在带有外部小键盘设备的专用计算机上运行。

另一个选择是接受NUMPAD ARROW KEYS(等)并将它们转换为NUMBERS,是否有可能? (例如,忽略numlock off情况并表现为numlock打开)

4 个答案:

答案 0 :(得分:3)

我不确定你是如何在vb.net(快速谷歌找到http://support.microsoft.com/kb/177674)中这样做的,但一般来说,更改这样的机器范围设置是不赞成的:用户习惯于他们的个人偏好对于numlock(或大写 - 或其他)关键状态...而你正在覆盖它。

这会真的激怒我。

也就是说,在您的应用程序是唯一运行的情况下(例如,POS软件,医疗办公管理软件等),这可能没问题。

答案 1 :(得分:2)

以物理方式删除NUMPAD密钥?

答案 2 :(得分:1)

    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()

答案 3 :(得分:0)

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