Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

SetKeyboardState (user32)
 
.
Summary

C# Signature:

[DllImport("user32.dll")]
static extern bool SetKeyboardState(byte [] lpKeyState);

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

Please add some!

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

VB.NET Signature:

[DllImport("user32.dll")]
static extern bool SetKeyboardState(byte [] lpKeyState);

User-Defined Types:

None.

Notes:

None.

Sample Code:

Public Class Form1

    ' struct to call API
    Private Structure OSVERSIONINFO

    Dim dwOSVersionInfoSize As Integer
    Dim dwMajorVersion As Integer
    Dim dwMinorVersion As Integer
    Dim dwBuildNumber As Integer
    Dim dwPlatformId As Integer

    <VBFixedString(128), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=128)> Public szCSDVersion As String '  Maintenance string for PSS usage

    End Structure

    Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (ByRef lpVersionInformation As OSVERSIONINFO) As Integer
    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
    Private Declare Function GetKeyboardState Lib "user32" (ByRef pbKeyState As Byte) As Integer
    Private Declare Function SetKeyboardState Lib "user32" (ByRef lppbKeyState As Byte) As Integer

    'const
    Const VK_NUMLOCK As Short = &H90S
    Const VK_SCROLL As Short = &H91S
    Const VK_CAPITAL As Short = &H14S
    Const KEYEVENTF_EXTENDEDKEY As Short = &H1S
    Const KEYEVENTF_KEYUP As Short = &H2S
    Const VER_PLATFORM_WIN32_NT As Short = 2
    Const VER_PLATFORM_WIN32_WINDOWS As Short = 1

    Private Sub cmdNumLock_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdNumLock.Click

    Dim o As OSVERSIONINFO
    Dim NumLockState As Boolean

    o.dwOSVersionInfoSize = Len(o)

    GetVersionEx(o)
    Dim keys(255) As Byte

    GetKeyboardState(keys(0))

    'NUMLOCK
    NumLockState = keys(VK_NUMLOCK)

    'check NUMLOCK state
    If NumLockState <> True Then 'if disabled then enable it
        If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=== Win95/98
        keys(VK_NUMLOCK) = 1
        SetKeyboardState(keys(0))
        ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then  '=== WinNT
        'Key Press
        keybd_event(VK_NUMLOCK, &H45S, KEYEVENTF_EXTENDEDKEY Or 0, 0)
        'Key Release
        keybd_event(VK_NUMLOCK, &H45S, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
        End If
    Else 'se estiver ativado -> if enabled then disable it
        If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=== Win95/98
        keys(VK_NUMLOCK) = 0
        SetKeyboardState(keys(0))
        ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then  '=== WinNT
        'Key Press
        keybd_event(VK_NUMLOCK, &H45S, KEYEVENTF_EXTENDEDKEY Or 0, 0)
        'Key Release
        keybd_event(VK_NUMLOCK, &H45S, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
        End If
    End If

    End Sub

    Private Sub cmdCapsLock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCapsLock.Click

    Dim o As OSVERSIONINFO
    Dim CapsLockState As Boolean

    o.dwOSVersionInfoSize = Len(o)

    GetVersionEx(o)
    Dim keys(255) As Byte

    GetKeyboardState(keys(0))

    'CAPSLOCK
    CapsLockState = keys(VK_CAPITAL)

    'check CAPSLOCK state
    If CapsLockState <> True Then 'if disabled then enable it
        If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=== Win95/98
        keys(VK_CAPITAL) = 1
        SetKeyboardState(keys(0))
        ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then  '=== WinNT
        'Key Press
        keybd_event(VK_CAPITAL, &H45S, KEYEVENTF_EXTENDEDKEY Or 0, 0)
        'Key Release
        keybd_event(VK_CAPITAL, &H45S, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
        End If
    Else 'se estiver ativado -> if enabled then disable it
        If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=== Win95/98
        keys(VK_CAPITAL) = 0
        SetKeyboardState(keys(0))
        ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then  '=== WinNT
        'Key Press
        keybd_event(VK_CAPITAL, &H45S, KEYEVENTF_EXTENDEDKEY Or 0, 0)
        'Key Release
        keybd_event(VK_CAPITAL, &H45S, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
        End If
    End If

    End Sub

End Class

Alternative Managed API: (VB.NET)

.NET 1.1: Método SendKeys (System.Windows.Forms)

.NET 2.0: My.Computer.Keyboard.SendKeys

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions