【问题标题】:How do I get the current state of Caps Lock in VB.NET?如何在 VB.NET 中获取 Caps Lock 的当前状态?
【发布时间】:2008-09-12 13:37:16
【问题描述】:

如何使用 VB.NET 确定 Caps Lock 是否被激活?

这是我的earlier question 的后续。

【问题讨论】:

    标签: .net windows vb.net


    【解决方案1】:

    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#版本:

    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.");
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      我不是 VB.NET 方面的专家,所以我只想到了 PInvoke:

      Declare Function GetKeyState Lib "user32" 
         Alias "GetKeyState" (ByValnVirtKey As Int32) As Int16
      
      Private Const VK_CAPSLOCK = &H14
      
      If GetKeyState(VK_CAPSLOCK) = 1 Then ...
      

      【讨论】:

        【解决方案3】:

        创建一个设置为 5 毫秒并已启用的计时器。
        然后制作一个名为label1 的标签。之后,尝试以下代码(在计时器事件处理程序中)。

        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            If My.Computer.Keyboard.CapsLock = True Then
                Label1.Text = "Caps Lock Enabled"
            Else
                Label1.Text = "Caps Lock Disabled"
            End If
        End Sub
        

        【讨论】:

          【解决方案4】:

          .rp 发布的解决方案有效,但与Me.KeyDown 事件处理程序冲突。
          我有一个 sub 在按下 enter 时调用登录功能(如下所示)。
          My.Computer.Keyboard.CapsLock 状态有效且与Me.Keydown 不冲突。

          Private Sub WindowLogin_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
          
              If Keyboard.IsKeyDown(Key.Enter) Then
                  Call SignIn()
              End If
          
          End Sub
          

          【讨论】:

            猜你喜欢
            • 2021-02-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-03-14
            相关资源
            最近更新 更多