2008-09-12 55 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