Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than user32, prefix the name with the module name and a period.
<DllImport("user32.dll", SetLastError := true)> _
<DllImport("user32.dll")> _
Private Shared Function GetKeyboardState(ByVal keyState() As Byte) As Boolean
End Function
'or...
Private Declare Function GetKeyboardState Lib "user32" (ByVal keyState() As Byte) As Boolean
Notes:
In some cases this function will always return the same array, independent of actual keyboard state. This is due to Windows not updating the virtual key array internally. It has been found that declaring and calling GetKeyState on any key before calling GetKeyboardState will solve this issue.
In some cases this function will always return the same array, independent of actual keyboard state. This is due to Windows not updating the virtual key array internally. It has been found that declaring and calling GetKeyState(0) before calling GetKeyboardState will solve this issue.
Tips & Tricks:
Please add some!
Sample Code:
C#:
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
namespace PInvokeSample {
public static class Keyboard {
public static int GetKeyState(){
byte[] keys = new byte[256];
//Get pressed keys
if(!NativeMethods.GetKeyboardState(keys)){
int err = Marshal.GetLastWin32Error();
throw new Win32Exception(err);
}
for(int i = 0; i < 256; i++){
byte key = keys[i];
//Logical 'and' so we can drop the low-order bit for toggled keys, else that key will appear with the value 1!
if((key & 0x80) != 0){
//This is just for a short demo, you may want this to return
//multiple keys!
return (int)key;
}
}
return -1;
}
}
}
using System;
using System.Runtime.InteropServices;
namespace PInvokeSample {
internal static class NativeMethods {
<DllImport("user32.dll")> _
Private Shared Function GetKeyboardState(ByVal keyState() As Byte) As Boolean
End Function
Private Shared Sub Update()
keyState = New Byte(256) {}
Dim result As Boolean = GetKeyboardState(keyState)
' Check for error:
If result = False Then
Debug.WriteLine("GetKeyBoardState error: " & Marshal.GetLastWin32Error)
Throw New Exception("GetKeyBoardState error: " & Marshal.GetLastWin32Error)
End If
End Sub
Public Enum LightState
Off
[On]
End Enum
' Example - the keyboard lights...
Public Shared ReadOnly Property CapsLockState() As LightState
Get
Update()
Dim isOn As Boolean = (keyState(Keys.CapsLock) = 1)
Return IIf(isOn, LightState.On, LightState.Off)
End Get
End Property
Public Shared ReadOnly Property NumLockState() As LightState
Get
Update()
Dim isOn As Boolean = (keyState(Keys.NumLock) = 1)
Return IIf(isOn, LightState.On, LightState.Off)
End Get
End Property
Public Shared ReadOnly Property ScrollLockState() As LightState
Get
Update()
Dim isOn As Boolean = (keyState(Keys.Scroll) = 1)
Return IIf(isOn, LightState.On, LightState.Off)
End Get
End Property
End Class
Alternative Managed API:
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace PInvoke.Net
{
public class Keyboard
{
[DllImport( "user32.dll" )]
static extern short GetKeyState( VirtualKeyStates nVirtKey );
public static bool IsKeyPressed( VirtualKeyStates testKey )
{
bool keyPressed = false;
short result = GetKeyState( testKey );
switch( result )
{
case 0:
// Not pressed and not toggled on.
keyPressed = false;
break;
case 1:
// Not pressed, but toggled on
keyPressed = false;
break;
default:
// Pressed (and may be toggled on)
keyPressed = true;
break;
}
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).