Disable Num Lock

Posted on 2009-02-21
Last Modified: 2012-05-06
How can i disable num lock on start up of the software and enable at close of software?
Or is there another solution?
Question by:Millkind
    LVL 15

    Accepted Solution

    try this
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        End Sub
        Private Const VER_PLATFORM_WIN32_NT = 2
        Private Const VER_PLATFORM_WIN32_WINDOWS = 1
        Private Const VK_NUMLOCK = &H90
        Private Const KEYEVENTF_EXTENDEDKEY = &H1
        Private Const KEYEVENTF_KEYUP = &H2
        Private Structure OSVERSIONINFO
            Dim dwOSVersionInfoSize As Long
            Dim dwMajorVersion As Long
            Dim dwMinorVersion As Long
            Dim dwBuildNumber As Long
            Dim dwPlatformId As Long
            Dim szCSDVersion As String
        End Structure
        ' API declarations:
        Private Declare Function GetVersionEx Lib "kernel32" _
           Alias "GetVersionExA" _
           (ByVal lpVersionInformation As OSVERSIONINFO) As Long
        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 Function GetKeyboardState Lib "user32" _
           (ByVal pbKeyState As Byte) As Long
        Private Declare Function SetKeyboardState Lib "user32" _
           (ByVal lppbKeyState As Byte) As Long
        Public Sub ToggleNumLock(ByVal TurnOn As Boolean)
            'To turn numlock on, set turnon to true
            'To turn numlock off, set turnon to false
            Dim bytKeys(255) As Byte
            Dim bnumLockOn As Boolean
            'Get status of the 256 virtual keys
            bnumLockOn = bytKeys(VK_NUMLOCK)
            Dim typOS As OSVERSIONINFO
            If bnumLockOn <> TurnOn Then 'if current state <>
                'requested stae
                If typOS.dwPlatformId = _
                    VER_PLATFORM_WIN32_WINDOWS Then  '=== Win95/98
                    bytKeys(VK_NUMLOCK) = 1
                Else    '=== WinNT/2000
                    'Simulate Key Press
                    keybd_event(VK_NUMLOCK, &H45, _
                       KEYEVENTF_EXTENDEDKEY Or 0, 0)
                    'Simulate Key Release
                    keybd_event(VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY _
                       Or KEYEVENTF_KEYUP, 0)
                End If
            End If
        End Sub

    Open in new window

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Do you just want to TOGGLE it?...or completely PREVENT it from being changed while your app is running?  The latter would require a low level keyboard hook where you could suppress the keystroke completely...

    Author Comment

    The later please.
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Ok...I haven't tested praveenvenu's code...but use it to turn NumLock OFF first.

    Then you can use code like below to keep it off:
    Imports System.Runtime.InteropServices
    Public Class Form1
        Private snl As SuppressNumLock
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            snl = New SuppressNumLock
        End Sub
        Private Class SuppressNumLock
            Private Const HC_ACTION As Integer = 0
            Private Const WH_KEYBOARD_LL As Integer = 13
            Private Const WM_KEYDOWN As Integer = &H100
            Private Const WM_KEYUP As Integer = &H101
            Private Const WM_SYSKEYDOWN As Integer = &H104
            Private Const WM_SYSKEYUP As Integer = &H105
            Public Structure KBDLLHOOKSTRUCT
                Public vkCode As Integer
                Public scancode As Integer
                Public flags As Integer
                Public time As Integer
                Public dwExtraInfo As Integer
            End Structure
            Private Declare Function SetWindowsHookEx Lib "user32" _
                Alias "SetWindowsHookExA" ( _
                ByVal idHook As Integer, _
                ByVal lpfn As LowLevelKeyboardProcDelegate, _
                ByVal hmod As Integer, _
                ByVal dwThreadId As Integer) As Integer
            Private Declare Function CallNextHookEx Lib "user32" ( _
                ByVal hHook As Integer, _
                ByVal nCode As Integer, _
                ByVal wParam As Integer, _
                ByVal lParam As KBDLLHOOKSTRUCT) As Integer
            Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
                ByVal hHook As Integer) As Integer
            Private Delegate Function LowLevelKeyboardProcDelegate( _
                ByVal nCode As Integer, _
                ByVal wParam As Integer, _
                ByRef lParam As KBDLLHOOKSTRUCT) As Integer
            Private hhkLowLevelKeyboard As Integer
            Private keyboardDelegate As LowLevelKeyboardProcDelegate
            Public Sub New()
                keyboardDelegate = New LowLevelKeyboardProcDelegate(AddressOf Me.LowLevelKeyboardProc)
                hhkLowLevelKeyboard = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardDelegate, _
                    Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
            End Sub
            Private Function LowLevelKeyboardProc( _
                ByVal nCode As Integer, _
                ByVal wParam As Integer, _
                ByRef lParam As KBDLLHOOKSTRUCT) As Integer
                If (nCode = HC_ACTION) Then
                    Select Case wParam
                            If DirectCast(lParam.vkCode, Keys) = Keys.NumLock Then
                                Return True ' Suppress Keystroke
                            End If
                    End Select
                End If
                Return CallNextHookEx(hhkLowLevelKeyboard, nCode, wParam, lParam)
            End Function
            Protected Overrides Sub Finalize()
            End Sub
        End Class
    End Class

    Open in new window


    Author Comment

    I'd like nums lock to be set to the on position for the duration of the software running.  No matter what the user does i want the nums lock to be on.  Sorry for any confusion this caused.
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    The function praveenvenu gave you can turn it ON or OFF.  Just pass True/False to his ToggleNumLock() method:

        Public Sub ToggleNumLock(ByVal TurnOn As Boolean)

    Then  MY code will keep the USER from pressing the NumLock key and toggling it.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    This video discusses moving either the default database or any database to a new volume.

    746 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

    16 Experts available now in Live!

    Get 1:1 Help Now