none
I cave! Could someone show me how to get my program to press the Num Lock key? RRS feed

  • Question

  • Sorry for asking in CLR but I figure this category probably has a lot of people that know what the are talking about.

     

    So I have spent all this time getting my program to work and it does work except it doesnt do what I want.

     

    My program listens for you to press the Num Lock key and then it presses the Num Lock key so that the Num Lock key doesnt toggle between numbers and arrows. The problem is that although it is apparantly pressing the Num Lock key it doesn't work. :(

    Forms.SendKey("{NUMLOCK}") does not work! Sure, it may tell the program in focus to press the Num Lock key but it doesnt actually toggle the Num Lock key. I need it to toggle the Num Lock key. How?

    Wednesday, May 5, 2010 8:45 PM

Answers

  • In order to press num lock, you'll need to call keybd_event via PInvoke.  SendKeys will not work.

    Here is a CodeProject article describing the process.  It also shows you how to detect the current state of the num lock key.


    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by SamAgain Thursday, May 13, 2010 1:00 AM
    Wednesday, May 5, 2010 8:54 PM
    Moderator
  • The .NET SendKeys are flaky.  Use the Windows Script Host one.  Reference it on the COM tab.

    Imports IWshRuntimeLibrary
    Public Class Form1
      
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
    Dim Wsh As New WshShell
        Wsh.SendKeys(
    "{NUMLOCK}")
      
    End Sub
    End
     Class

    • Marked as answer by SamAgain Thursday, May 13, 2010 1:00 AM
    Wednesday, May 5, 2010 10:38 PM

All replies

  • In order to press num lock, you'll need to call keybd_event via PInvoke.  SendKeys will not work.

    Here is a CodeProject article describing the process.  It also shows you how to detect the current state of the num lock key.


    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by SamAgain Thursday, May 13, 2010 1:00 AM
    Wednesday, May 5, 2010 8:54 PM
    Moderator
  • The .NET SendKeys are flaky.  Use the Windows Script Host one.  Reference it on the COM tab.

    Imports IWshRuntimeLibrary
    Public Class Form1
      
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
    Dim Wsh As New WshShell
        Wsh.SendKeys(
    "{NUMLOCK}")
      
    End Sub
    End
     Class

    • Marked as answer by SamAgain Thursday, May 13, 2010 1:00 AM
    Wednesday, May 5, 2010 10:38 PM