ホーム › UI.Input.KeyboardAndMouse › GetKeyboardState
GetKeyboardState
関数全仮想キーの状態を配列としてまとめて取得する。
シグネチャ
// USER32.dll
#include <windows.h>
BOOL GetKeyboardState(
BYTE* lpKeyState
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| lpKeyState | BYTE* | out |
戻り値の型: BOOL
各言語での呼び出し定義
// USER32.dll
#include <windows.h>
BOOL GetKeyboardState(
BYTE* lpKeyState
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool GetKeyboardState(
IntPtr lpKeyState // BYTE* out
);<DllImport("USER32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetKeyboardState(
lpKeyState As IntPtr ' BYTE* out
) As Boolean
End Function' lpKeyState : BYTE* out
Declare PtrSafe Function GetKeyboardState Lib "user32" ( _
ByVal lpKeyState As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
GetKeyboardState = ctypes.windll.user32.GetKeyboardState
GetKeyboardState.restype = wintypes.BOOL
GetKeyboardState.argtypes = [
ctypes.POINTER(ctypes.c_ubyte), # lpKeyState : BYTE* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('USER32.dll')
GetKeyboardState = Fiddle::Function.new(
lib['GetKeyboardState'],
[
Fiddle::TYPE_VOIDP, # lpKeyState : BYTE* out
],
Fiddle::TYPE_INT)#[link(name = "user32")]
extern "system" {
fn GetKeyboardState(
lpKeyState: *mut u8 // BYTE* out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", SetLastError = true)]
public static extern bool GetKeyboardState(IntPtr lpKeyState);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_GetKeyboardState' -Namespace Win32 -PassThru
# $api::GetKeyboardState(lpKeyState)#uselib "USER32.dll"
#func global GetKeyboardState "GetKeyboardState" sptr
; GetKeyboardState varptr(lpKeyState) ; 戻り値は stat
; lpKeyState : BYTE* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "USER32.dll" #cfunc global GetKeyboardState "GetKeyboardState" var ; res = GetKeyboardState(lpKeyState) ; lpKeyState : BYTE* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "USER32.dll" #cfunc global GetKeyboardState "GetKeyboardState" sptr ; res = GetKeyboardState(varptr(lpKeyState)) ; lpKeyState : BYTE* out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; BOOL GetKeyboardState(BYTE* lpKeyState) #uselib "USER32.dll" #cfunc global GetKeyboardState "GetKeyboardState" var ; res = GetKeyboardState(lpKeyState) ; lpKeyState : BYTE* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL GetKeyboardState(BYTE* lpKeyState) #uselib "USER32.dll" #cfunc global GetKeyboardState "GetKeyboardState" intptr ; res = GetKeyboardState(varptr(lpKeyState)) ; lpKeyState : BYTE* out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
user32 = windows.NewLazySystemDLL("USER32.dll")
procGetKeyboardState = user32.NewProc("GetKeyboardState")
)
// lpKeyState (BYTE* out)
r1, _, err := procGetKeyboardState.Call(
uintptr(lpKeyState),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction GetKeyboardState(
lpKeyState: Pointer // BYTE* out
): BOOL; stdcall;
external 'USER32.dll' name 'GetKeyboardState';result := DllCall("USER32\GetKeyboardState"
, "Ptr", lpKeyState ; BYTE* out
, "Int") ; return: BOOL●GetKeyboardState(lpKeyState) = DLL("USER32.dll", "bool GetKeyboardState(void*)")
# 呼び出し: GetKeyboardState(lpKeyState)
# lpKeyState : BYTE* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。