Disable Num Lock

Posted on 2009-02-21
Medium Priority
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
  • 3
  • 2
LVL 15

Accepted Solution

Praveen Venu earned 1000 total points
ID: 23700611
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_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 86

Expert Comment

by:Mike Tomlinson
ID: 23700775
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

ID: 23700889
The later please.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23701201
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

ID: 23701352
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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1000 total points
ID: 23701907
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Screencast - Getting to Know the Pipeline
Suggested Courses

807 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