Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the current state of Caps Lock in VB.NET?

How do I find out whether or not Caps Lock is activated, using VB.NET?

This is a follow-up to my earlier question.

like image 676
Luke Girvin Avatar asked Sep 12 '08 13:09

Luke Girvin


People also ask

How do I check Caps Lock?

Detect if Caps Lock is On Try to press the "Caps Lock" key inside the input field: WARNING! Caps lock is ON.

How do you know that Caps Lock key is on or off answer?

How to tell whether Caps Lock is on. If you pressed the Caps Lock key, you can turn it off by pressing the key again. Many keyboards have a built-in status indicator that lights up when the Caps Lock key is on. This LED is located either directly on the key or on the status bar of the keyboard, if there is one.

Which light indicates the status of Caps Lock?

The green LED on the key is lit, indicating that Caps Lock is on.

What is Caps Lock function?

A keyboard key that toggles upper case on and off. When the Caps Lock key is on, pressing any key automatically delivers the shifted version of the key, except for numeric digits, periods, commas, slashes and backslashes.


2 Answers

Control.IsKeyLocked(Keys) Method - MSDN

Imports System
Imports System.Windows.Forms
Imports Microsoft.VisualBasic

Public Class CapsLockIndicator

    Public Shared Sub Main()
        if Control.IsKeyLocked(Keys.CapsLock) Then
            MessageBox.Show("The Caps Lock key is ON.")
        Else
            MessageBox.Show("The Caps Lock key is OFF.")
        End If
    End Sub 'Main
End Class 'CapsLockIndicator

C# version:

using System;
using System.Windows.Forms;

public class CapsLockIndicator
{
    public static void Main()
    {
        if (Control.IsKeyLocked(Keys.CapsLock)) {
            MessageBox.Show("The Caps Lock key is ON.");
        }
        else {
            MessageBox.Show("The Caps Lock key is OFF.");
        }
    }
}
like image 55
rp. Avatar answered Oct 16 '22 14:10

rp.


I'm not an expert in VB.NET so only PInvoke comes to my mind:

Declare Function GetKeyState Lib "user32" 
   Alias "GetKeyState" (ByValnVirtKey As Int32) As Int16

Private Const VK_CAPSLOCK = &H14

If GetKeyState(VK_CAPSLOCK) = 1 Then ...
like image 30
aku Avatar answered Oct 16 '22 16:10

aku