• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 474
  • Last Modified:

Get Scroll Lock State / Set Scroll Lock State. How?

Hello Experts,

I was trying to build an app that does the following:

1. On form load call timer1.
2. Timer1 will tick every 5 seconds and query Scroll Lock State(ON - OFF).
2.1. If Scroll Lock State is OFF Turn ON Else If Scroll Lock State is ON Turn OFF.

I'm looking for the smalles amount of code to acomplish this but all suggestions are welcome!

Hope that makes sense.
0
donniedarko801
Asked:
donniedarko801
  • 4
  • 3
1 Solution
 
ElrondCTCommented:
In VB .NET 2002/2003, you have to use a Win API. Here's a link for code to do that: http://www.a1vbcode.com/snippet-3541.asp

With VB 2005, the scroll lock state can be read using the My.Computer.Keyboard.ScrollLock property, which is a boolean. However, the property is read-only, and it doesn't look there's a way to change the state with a built-in VB function or method. So I guess you'd still need to use the code described above, or something like it, to actually toggle the scroll lock.
0
 
donniedarko801Author Commented:
Ok. I'm behind a firewall right now. Can you post some code displaying how you get the scroll lock state and return a msgbox if on or off? Thank you!
0
 
ElrondCTCommented:
Taken from the previously mentioned website (http://www.a1vbcode.com/snippet-3541.asp):

'Declare these in a module somewhere
Public Const VK_NUMLOCK As Short = &H90S
Public Const VK_SCROLL As Short = &H91S
Public Const VK_CAPITAL As Short = &H14S
Public Const KEYEVENTF_EXTENDEDKEY As Short = &H1S
Public Const KEYEVENTF_KEYUP As Short = &H2S
Public Const VER_PLATFORM_WIN32_NT As Short = 2
Public Const VER_PLATFORM_WIN32_WINDOWS As Short = 1

'Copy this code into a new VB file
Namespace KeyboardFunctions
  Public Enum KeyToggleStatus As Integer
    StatusOn = 0
    StatusOff
  End Enum
  Public Enum ToggleKey As Integer
    CapsLock = 0
    NumLock
    ScrollLock
  End Enum

  Public Class ToggleKeyControl

    Private Structure OSVERSIONINFO
      Dim dwOSVersionInfoSize As Integer
      Dim dwMajorVersion As Integer
      Dim dwMinorVersion As Integer
      Dim dwBuildNumber As Integer
      Dim dwPlatformId As Integer
      <VBFixedString(128), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=128)> Public szCSDVersion As String '  Maintenance string for PSS usage
    End Structure

    ' API declarations:

    'UPGRADE_WARNING: Structure OSVERSIONINFO may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1050"'
    Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (ByRef lpVersionInformation As OSVERSIONINFO) As Integer
    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
    Private Declare Function GetKeyboardState Lib "user32" (ByRef pbKeyState As Byte) As Integer
    Private Declare Function SetKeyboardState Lib "user32" (ByRef lppbKeyState As Byte) As Integer

    Public Shared Sub SetLock(ByVal targetKey As ToggleKey, ByVal status As KeyToggleStatus)
      Dim o As OSVERSIONINFO
      o.dwOSVersionInfoSize = Len(o)
      GetVersionEx(o)
      Dim keys(255) As Byte
      Dim index As Short
      GetKeyboardState(keys(0))
      If GetLockStatus(targetKey) = status Then
        'Nothing to do as the required status is already set
      Else
        Select Case targetKey
          Case ToggleKey.CapsLock : index = VK_CAPITAL
          Case ToggleKey.NumLock : index = VK_NUMLOCK
          Case ToggleKey.ScrollLock : index = VK_SCROLL
        End Select
        If status = KeyToggleStatus.StatusOn Then
          If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=== Win95/98
            keys(index) = 1
            SetKeyboardState(keys(0))
          ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then  '=== WinNT
            'Simulate Key Press
            keybd_event(index, &H45S, KEYEVENTF_EXTENDEDKEY Or 0, 0)
            'Simulate Key Release
            keybd_event(index, &H45S, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
          End If
        Else
          If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=== Win95/98
            keys(index) = 0
            SetKeyboardState(keys(0))
          ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then  '=== WinNT
            'Simulate Key Press
            keybd_event(index, &H45S, KEYEVENTF_EXTENDEDKEY Or 0, 0)
            'Simulate Key Release
            keybd_event(index, &H45S, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
          End If
        End If
      End If
    End Sub

    Public Shared Function GetLockStatus(ByVal targetKey As ToggleKey) As KeyboardFunctions.KeyToggleStatus
      Dim LockState As Boolean = False
      Dim keys(255) As Byte
      GetKeyboardState(keys(0))
      Select Case targetKey
        Case ToggleKey.CapsLock : LockState = keys(VK_CAPITAL)
        Case ToggleKey.NumLock : LockState = keys(VK_NUMLOCK)
        Case ToggleKey.ScrollLock : LockState = keys(VK_SCROLL)
      End Select
      Return ToKeyToggleStatus(LockState)
    End Function

    Public Shared Function ToKeyToggleStatus(ByVal value As Boolean) As KeyToggleStatus
      If value Then
        Return KeyToggleStatus.StatusOn
      Else
        Return KeyToggleStatus.StatusOff
      End If
    End Function
  End Class
End Namespace
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ElrondCTCommented:
Once you've put the above into a file in your application (you can put the module code inside the Namespace), you can find out a lock state with a line like:

If KeyboardFunctions.ToggleKeyControl.GetLockStatus(KeyboardFunctions.ToggleKey.ScrollLock) Then
   ' do whatever
End If

You can set a state with:

        KeyboardFunctions.ToggleKeyControl.SetLock(KeyboardFunctions.ToggleKey.ScrollLock, _
              KeyboardFunctions.KeyToggleStatus.StatusOn)

So, in your case, to toggle the state of Scroll Lock every 5 seconds, set up a Timer control with an interval of 5000, and enable it. For your your Timer Tick event:

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        KeyboardFunctions.ToggleKeyControl.SetLock(KeyboardFunctions.ToggleKey.ScrollLock, _
              Not KeyboardFunctions.ToggleKeyControl.GetLockStatus(KeyboardFunctions.ToggleKey.ScrollLock))
    End Sub
0
 
donniedarko801Author Commented:
Thanks so much.

Just curious...

How would you do it with the following code you mentioned and return a msgbox if on or off? I understand the following does not set the state as you mentioned previously...

My.Computer.Keyboard.ScrollLock
0
 
donniedarko801Author Commented:
Also is Could Send Keys be an option to toggle scroll lock on/off? Just thinking of different aproaches...
0
 
ElrondCTCommented:
I tried using SendKeys to toggle Scroll Lock, and it didn't seem to work for me.

For a messagebox, you'd use GetLockStatus from this code, so it might be something like:

If KeyboardFunctions.ToggleKeyControl.GetLockStatus(KeyboardFunctions.ToggleKey.ScrollLock) Then
   strStatus = "On"
Else
   strStatus = "Off"
End If
MessageBox.Show("Scroll Lock is " & strStatus)

If you're using VB 2005, the If statement could be:

If My.Computer.Keyboard.ScrollLock Then ...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now