2008-09-12 96 views

回答

12

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

创建设置为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 
0

.rp发布的解决方案有效,但与Me.KeyDown事件处理程序冲突。我有一个在按下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 
相关问题