如何获得VB.NET中Caps Lock的当前状态?

如何使用VB.NET找出Caps Lock是否被激活?

这是我先前问题的后续行动。

Solutions Collecting From Web of "如何获得VB.NET中Caps Lock的当前状态?"

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.iskeylocked.aspx

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

我不是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 ... 

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

  Public Class Form1 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 End Class 

.rp发布的解决方案工作,但与Me.KeyDown事件处理程序冲突。 我有一个小组,当输入按下时调用登录功能。 (如下所示)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