Changing Mouse Settings With VB.NET

Curiosity killed the cat, meaning: being too curious can be dangerous. Why am I mentioning this? Well, I'm a curious person, I always want to know what makes something tick; in today's lesson, you will say that I wanted to know what goes on behind the scenes when changing Mouse settings, how is it done, where are these settings? Those are just a few questions I will answer today. I will show you how to change all the Mouse settings through the use of your Visual Basic program and the Registry.

Highlights include:

  1. Changing Mouse Wheel settings
  2. Swap the Mouse buttons (Right handed/ Left Handed)
  3. Change double-click speed
  4. Click Lock
  5. Mouse Pointer Trails
  6. Mouse Pointer Shadows
  7. Snap to default button
  8. Hide the cursor while typing
  9. Mouse Pointer speed

Basically, all the settings are found in the Mouse control panel applet.

                 

The Design

I have designed my form to look like Figure 1. Feel free to give all objects your own descriptive names.

Figure 1: Design

The Coding

All the Mouse settings are stored in two main Registry keys:

  1. HKEY_CURRENT_USER\Control Panel\Desktop
  2. HKEY_CURRENT_USER\Control Panel\Mouse

To obtain these settings and to change them, you need to Import the Microsoft.Win32 namespace so that you can get access to all its Registry functions. You'll start with the Mouse Wheel Scroll settings.

Changing Mouse Settings With VB.NET

Mouse Wheel Scrolling

Add the following code to the button that gets the Mouse Wheel scrolling settings:

Private Sub btnGetWheelScroll_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnGetWheelScroll.Click
'Open Registry Key
Dim NewKey As RegistryKey = _
   Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", True)
'Get Value
Dim GetVal As String = _
   CType(NewKey.GetValue("WheelScrollLines"), String)

txtMouseWheel.Text = GetVal    'Update Display
If GetVal > 25 Then            'If Bigger Than 25 Lines At A Time
txtMouseWheel.Text = "One Page At A Time"
End If
End Sub

What you did here was to create a Registry object, and with this Registry object, you open the HKEY_CURRENT_USER\Control Panel key. You looked for a setting called WheelScrollLines, which is responsible for this mouse feature, and displayed its value in the txtMouseWheel textbox. If the WheelScrollLines setting is larger than 25, it will display "One Page At A Time".

Add the following to change WheelScrollLines:

Private Sub btnChangeWheelScroll_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnChangeWheelScroll.Click
'Open Registry Key
Dim NewKey As RegistryKey = _
   Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", True)

'Change Scroll Lines Amount
NewKey.SetValue("WheelScrollLines", txtMouseWheel.Text)
End Sub

Here, you open the appropriate registry key again, and set its value to whatever text was entered in txtMouseWheel.

Swapping Mouse Buttons

This setting affects whether or not you are left handed or right handed. Create the following Private variable:

Private MouseButt As Integer    'Which Mouse Button?

In the Left Handed and Right Handed Radio Buttons'CheckedChanged events, add the following:

Private Sub rdLeftButton_CheckedChanged(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles rdLeftButton.CheckedChanged
MouseButt = 1    'Left Handed
End Sub

Private Sub rdRightButton_CheckedChanged(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles rdRightButton.CheckedChanged
MouseButt = 0    'Right Handed
End Sub

A setting of 1 indicates that the user is left handed, and a setting of 0 indicates that the user is right handed. To get the current system setting, add the following to this section's Get Setting button:

Private Sub btnGetMouseButton_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnGetMouseButton.Click
'Open Registry Key
Dim NewKey As RegistryKey = _
   Registry.CurrentUser.OpenSubKey("Control Panel\\Mouse", True)

'Get Value
Dim GetVal As String = _
   CType(NewKey.GetValue("SwapMouseButtons"), String)
Select Case GetVal
Case "0"    'Right Handed?
rdRightButton.Checked = True    'Update Display
rdLeftButton.Checked  = False
Case "1"    'Left Handed?
rdLeftButton.Checked  = True
rdRightButton.Checked = False
End Select
End Sub

Here, you looked into the SwapMouseButtons setting, and depending on its value, the appropriate radio button would be checked/unchecked. To change this setting, add the following:

Private Sub btnChangeMouseButton_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnChangeMouseButton.Click
'Open Registry Key
Dim NewKey As RegistryKey = _
   Registry.CurrentUser.OpenSubKey("Control Panel\\Mouse", True)

'Change Left Handed/Right Handed
NewKey.SetValue("SwapMouseButtons", MouseButt.ToString())
End Sub

You simply wrote the current selected radiobutton's value to SwapMouseButtons—that's it!

Interestingly, you can change this setting via the use of RunDLL32 and User32.dll. The basic breakdown of the command would be:

Process.Start("rundll32.exe", "user32.dll, SwapMouseButton")

Changing the Double-Click Speed

To get the double-click speed, all you need to do is to interrogate the DoubleClickSpeed setting inside HKEY_CURRENT_USER\Control Panel\Mouse. To change it, you just need to write a value to it.

Private Sub btnGetDoubleClickSpeed_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnGetDoubleClickSpeed.Click
'Open Registry Key
Dim NewKey As RegistryKey = _
   Registry.CurrentUser.OpenSubKey("Control Panel\\Mouse", True)
'Get Value
Dim GetVal As String = _
   CType(NewKey.GetValue("DoubleClickSpeed"), String)

'Update Appropriate TextBox Display
txtDoubleClickSpeed.Text = GetVal
End Sub

Private Sub btnChangeDoubleClickSpeed_Click(ByVal sender _
   As System.Object, _
   ByVal e As System.EventArgs) Handles btnChangeDoubleClickSpeed.Click
'Open Registry Key
Dim NewKey As RegistryKey = _
   Registry.CurrentUser.OpenSubKey("Control Panel\\Mouse", True)
'Set Double-Click Speed
NewKey.SetValue("DoubleClickSpeed", txtDoubleClickSpeed.Text)

'Max (Fastest) = 200
'Min (Slowest) = 900
End Sub

The value for DoubleClickSpeed can be any value between 200 (fastest) and 900 (slowest).

Applying a Click Lock

This is where these settings get a bit tricky. To turn Click Lock on, you need to modify a Registry setting named UserPreferencesMask inside HKEY_CURRENT_USER\Control Panel\Desktop. The reason why I say it can get a bit tricky is because this setting deals with Binary values. So, to interrogate its value, you need to cast this value's columns appropriately, and to set it, you need to change the appropriate binary column to the correct cast value. Here's how it is done:

Private Sub btnGetClickLock_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnGetClickLock.Click
'Open Registry Key
Dim NewKey As RegistryKey = _
   Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", True)

'Check Type Of Value In UserPreferencesMask
If NewKey.GetValueKind("UserPreferencesMask") = _
   RegistryValueKind.Binary Then
'Get Current Value
Dim data As Byte() = _
   DirectCast(NewKey.GetValue("UserPreferencesMask"), Byte())

If data(1) = 190 Then          'Click Lock Active?
chkClickLock.Checked = True    'Update Display
Else
chkClickLock.Checked = False
End If
End If

End Sub

'Open Registry Key
Private Sub btnChangeClickLock_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnChangeClickLock.Click
Dim NewKey As RegistryKey = _
   Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", True)

'Check Type Of Value In UserPreferencesMask
If NewKey.GetValueKind("UserPreferencesMask") = _
   RegistryValueKind.Binary Then
'Get Current Value
Dim data As Byte() = _
   DirectCast(NewKey.GetValue("UserPreferencesMask"), Byte())

data(1) = 190                                   ' Activate Click Lock
NewKey.SetValue("UserPreferencesMask", data)    'Update Registry
End If
End Sub

Changing Mouse Settings With VB.NET

Changing Mouse Pointer Trails

You need to determine whether the MouseTrails setting under the Control Panel\Mouse subkey is set, and then make use of that setting. To change it, you need to supply any value between 2 (shortest ) and 7 ( longest ), as shown with the following code:

Private Sub btnGetMouseTrails_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnGetMouseTrails.Click
'Open Registry Key
Dim NewKey As RegistryKey = _
   Registry.CurrentUser.OpenSubKey("Control Panel\\Mouse", True)

'Check For Values
If Not NewKey.GetValue("MouseTrails") = _
   String.Empty AndAlso NewKey.GetValue("MouseTrails") > 0 Then

'Get Setting
Dim GetVal As String = CType(NewKey.GetValue("MouseTrails"), String)
chkMouseTrails.Checked = True    'Update Display
txtMouseTrails.Text = GetVal
End If
End Sub

Private Sub btnChangeMouseTrails_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnChangeMouseTrails.Click
'Open Registry Key
Dim NewKey As RegistryKey = _
   Registry.CurrentUser.OpenSubKey("Control Panel\\Mouse", True)

'Activate Mouse Trails & Length Of Trails
If chkMouseTrails.Checked Then _
   NewKey.SetValue("MouseTrails", txtMouseTrails.Text)
'Max = 7
'Min = 2
End Sub

Enabling the Mouse Pointer Shadow

Here, you should use the UserPreferencesMask Binary setting again. A value of 62 would mean that the Mouse Pointer shadow is active, and a value of 30 would mean it is inactive. Here's the code:

Private Sub btnGetPointerShadow_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnGetPointerShadow.Click
'Open Registry Key
Dim NewKey As RegistryKey = _
   Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", True)

'Check Type Of Value In UserPreferencesMask
If NewKey.GetValueKind("UserPreferencesMask") = _
   RegistryValueKind.Binary Then
'Get Current Value
Dim data As Byte() = _
   DirectCast(NewKey.GetValue("UserPreferencesMask"), Byte())

If data(1) = 62 Then        'Pointer Shadow Active?
chkPointerShadow.Checked = True
ElseIf data(1) = 30 Then    'Pointer Shadow Inactive?
chkPointerShadow.Checked = False
End If
End If
End Sub

Private Sub btnChangePointerShadow_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnChangePointerShadow.Click
'Open Registry Key
Dim NewKey As RegistryKey = _
   Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", True)
'Check Type Of Value In UserPreferencesMask
If NewKey.GetValueKind("UserPreferencesMask") = _
   RegistryValueKind.Binary Then
'Get Current Value
Dim data As Byte() = _
   DirectCast(NewKey.GetValue("UserPreferencesMask"), Byte())
data(1) = 62 'Set To 62 ( Activate Pointer Shadow )

NewKey.SetValue("UserPreferencesMask", data)    'Update Registry
End If
End Sub

Changing the Mouse Pointer Speed

This is done via the use of the MouseSpeed setting under Control Panel\Mouse subkey, as in the following:

Private Sub btnGetMouseSpeed_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnGetMouseSpeed.Click
'Open Registry Key
Dim NewKey As RegistryKey = _
   Registry.CurrentUser.OpenSubKey("Control Panel\\Mouse", True)

'Get Current Value
Dim GetVal As String = CType(NewKey.GetValue("MouseSpeed"), String)

txtMouseSpeed.Text = GetVal    'Update Appropriate TextBox Display
End Sub

Private Sub btnChangeMouseSpeed_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnChangeMouseSpeed.Click
'Open Registry Key
Dim NewKey As RegistryKey = _
   Registry.CurrentUser.OpenSubKey("Control Panel\\Mouse", True)

'Specify & Activate Mouse Speed
NewKey.SetValue("MouseSpeed", txtMouseSpeed.Text)
'Max (Fastest) = 20
'Min (Slowest) = 0

End Sub

Any value between 0 (slowest) and 20 (fastest) is acceptable.

Snap to Default Button

Here, you also need to create a variable containing 0 or 1, and then determine or set it appropriately.

Private SnapTo As Integer    'Snap To, Yes/No

In the Snap To default button checkbox's CheckedChanged event, ensure that when it is checked your private SnapTo variable becomes 1; else, 0.

Private Sub chkSnapToDefault_CheckedChanged(ByVal sender _
   As System.Object, _
   ByVal e As System.EventArgs) Handles chkSnapToDefault.CheckedChanged
If chkSnapToDefault.Checked Then    'Activate/Deactivate
SnapTo = 1
Else
SnapTo = 0
End If
End Sub

Private Sub btnGetSnapToDefault_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnGetSnapToDefault.Click
'Open Registry Key
Dim NewKey As RegistryKey = _
   Registry.CurrentUser.OpenSubKey("Control Panel\\Mouse", True)
'Get Current Value
Dim GetVal As String = _
   CType(NewKey.GetValue("SnapToDefaultButton"), String)

Select Case GetVal
Case "0"    'Inactive SnapTo
chkSnapToDefault.Checked = False
Case "1"    'Active SnapTo
chkSnapToDefault.Checked = True
End Select
End Sub

Private Sub btnChangeSnapToDefault_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnChangeSnapToDefault.Click
'Open Registry Key
Dim NewKey As RegistryKey = _
   Registry.CurrentUser.OpenSubKey("Control Panel\\Mouse", True)
'Activate SnapTo Default Button
NewKey.SetValue("SnapToDefaultButton", SnapTo.ToString)
End Sub

Activating the Pointer Location when CTRL is Pressed Setting

This is how to activate and obtain this setting.

Private Sub btngetPointerLoc_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btngetPointerLoc.Click
'Open Registry Key
Dim NewKey As RegistryKey = _
   Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", True)

'Check Type Of Value In UserPreferencesMask
If NewKey.GetValueKind("UserPreferencesMask") = _
   RegistryValueKind.Binary Then
'Get The Current Setting
Dim data As Byte() = _
   DirectCast(NewKey.GetValue("UserPreferencesMask"), Byte())

'Pointer Location When CTRL Is Pressed Active?
If data(1) = 126 Then

chkPointerLoc.Checked = True    'Update Display
Else
chkPointerLoc.Checked = False
End If
End If
End Sub

Private Sub btnChangePointerLoc_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnChangePointerLoc.Click
'Open Registry Key
Dim NewKey As RegistryKey = _
   Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", True)

'Check Type Of Value In UserPreferencesMask
If NewKey.GetValueKind("UserPreferencesMask") = _
   RegistryValueKind.Binary Then
'Get The Current Setting
Dim data As Byte() = _
   DirectCast(NewKey.GetValue("UserPreferencesMask"), Byte())

'Activate Pointer Location When CTRL Is Pressed Setting
data(1) = 126

NewKey.SetValue("UserPreferencesMask", data)    'Update Registry
End If
End Sub

Changing Mouse Settings With VB.NET

Activating Hide Cursor while Typing

Private Sub btnGetHidePointer_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnGetHidePointer.Click
'Open Registry Key
Dim NewKey As RegistryKey = _
   Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", True)

'Check Type Of Value In UserPreferencesMask
If NewKey.GetValueKind("UserPreferencesMask") = _
   RegistryValueKind.Binary Then
'Get The Current Setting
Dim data As Byte() = _
   DirectCast(NewKey.GetValue("UserPreferencesMask"), Byte())

If data(2) = 7 Then        'Hide Pointer Active?
chkHidePointer.Checked = True

ElseIf data(2) = 6 Then    'Hide Pointer When Inactive
chkHidePointer.Checked = False
End If
End If
End Sub

Private Sub btnChangeHidePointer_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnChangeHidePointer.Click
'Open Registry Key
Dim NewKey As RegistryKey = _
   Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", True)

'Check Type Of Value In UserPreferencesMask
If NewKey.GetValueKind("UserPreferencesMask") = _
   RegistryValueKind.Binary Then

'Get The Current Setting
Dim data As Byte() = _
   DirectCast(NewKey.GetValue("UserPreferencesMask"), Byte())
data(2) = 7 'Set To 7 ( Activate Hide Pointer While Typing )

NewKey.SetValue("UserPreferencesMask", data)    'Update Registry
End If
End Sub

Loading System Cursors

Add the following function to your form:

Private Function CursorList() As Cursor()
' Array Of All Cursors In Windows Forms.
Return New Cursor() {Cursors.AppStarting, Cursors.Arrow, _
   Cursors.Cross, Cursors.Default, Cursors.Hand, Cursors.Help, _
   Cursors.HSplit, Cursors.IBeam, Cursors.No, _
   Cursors.NoMove2D, Cursors.NoMoveHoriz, Cursors.NoMoveVert, _
   Cursors.PanEast, Cursors.PanNE, Cursors.PanNorth, _
   Cursors.PanNW, Cursors.PanSE, Cursors.PanSouth, _
   Cursors.PanSW, Cursors.PanWest, Cursors.SizeAll, _
   Cursors.SizeNESW, Cursors.SizeNS, Cursors.SizeNWSE, _
   Cursors.SizeWE, Cursors.UpArrow, Cursors.VSplit, Cursors.WaitCursor}
End Function

The purpose of this function is to create a list of all available cursors, from which you can use a For loop to load all these cursors into your cursor combobox.

Private Sub frmMouseSettings_Load(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles MyBase.Load
Dim MouseCursor As Cursor                  'Create Cursor Object

For Each MouseCursor In CursorList()       'Loop Through Each Cursor
cboMousePointers.Items.Add(MouseCursor)    'Add to comboBox
Next MouseCursor

End Sub

Apply the Selected Cursor

Private Sub cboMousePointers_SelectedIndexChanged(ByVal sender _
   As System.Object, ByVal e As System.EventArgs) _
   Handles cboMousePointers.SelectedIndexChanged

'Change Cursor To Seledcted Cursor
Me.Cursor = CType(cboMousePointers.SelectedItem, Cursor)

End Sub

This only applies the selected cursor to the form. To set it system wide, you will need an API called SetCursor

Note: You may expect that all these settings will be applied automatically; unfortunately, that is not not the case. Only a reboot of the computer will update your changed settings. For these settings to applied immediately, you would have to restart explorer.exe. To achieve this, you will need to use the SendMessageTimeout API. Due to time constraints, this is not included in this article, but watch out for my following article that deals with TaskBar settings.

Conclusion

Well, that is it for this topic. I really do hope that you have enjoyed this article as much as I did, and that you have benefited from its content. Until next time!



About the Author

Hannes du Preez

Hannes du Preez is a Microsoft MVP for Visual Basic. He is a trainer at a South African-based company. He is the co-founder of hmsmp.co.za, a community for South African developers.

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 13, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT APIs can be a great source of competitive advantage. The practice of exposing backend services as APIs has become pervasive, however their use varies widely across companies and industries. Some companies leverage APIs to create internal, operational and development efficiencies, while others use them to drive ancillary revenue channels. Many companies successfully support both public and private programs from the same API by varying levels …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds