如何在VB.NET中切换Caps Lock?

时间:2008-09-12 13:22:03

标签: .net windows vb.net

使用VB.NET,如何切换Caps Lock的状态?

3 个答案:

答案 0 :(得分:6)

尝试this

Public Class Form1
    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Call keybd_event(System.Windows.Forms.Keys.CapsLock, &H14, 1, 0)
            Call keybd_event(System.Windows.Forms.Keys.CapsLock, &H14, 3, 0)
        End Sub
End Class

答案 1 :(得分:5)

来自:http://www.vbforums.com/showthread.php?referrerid=61394&t=537891

Imports System.Runtime.InteropServices

Public Class Form2

    Private Declare Sub keybd_event Lib "user32" ( _
        ByVal bVk As Byte, _
        ByVal bScan As Byte, _
        ByVal dwFlags As Integer, _
        ByVal dwExtraInfo As Integer _
    )
    Private Const VK_CAPITAL As Integer = &H14
    Private Const KEYEVENTF_EXTENDEDKEY As Integer = &H1
    Private Const KEYEVENTF_KEYUP As Integer = &H2

    Private Sub Button1_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs _
    ) Handles Button1.Click

        ' Toggle CapsLock

        ' Simulate the Key Press
        keybd_event(VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0)

        ' Simulate the Key Release
        keybd_event(VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
    End Sub

End Class 

答案 2 :(得分:0)

我用这个

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
Private Const KEYEVENTF_EXTENDEDKEY As Integer = &H1
Private Const KEYEVENTF_KEYUP As Integer = &H2
'put this where you want to turn caps lock on or off
keybd_event(VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0)
keybd_event(VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)