Win32 API 日本語リファレンス
ホームUI.Input.KeyboardAndMouse › GetKeyNameTextW

GetKeyNameTextW

関数
スキャンコードに対応するキー名の文字列を取得する(Unicode版)。
DLLUSER32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

// USER32.dll  (Unicode / -W)
#include <windows.h>

INT GetKeyNameTextW(
    INT lParam,
    LPWSTR lpString,
    INT cchSize
);

パラメーター

名前方向
lParamINTin
lpStringLPWSTRout
cchSizeINTin

戻り値の型: INT

各言語での呼び出し定義

// USER32.dll  (Unicode / -W)
#include <windows.h>

INT GetKeyNameTextW(
    INT lParam,
    LPWSTR lpString,
    INT cchSize
);
[DllImport("USER32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern int GetKeyNameTextW(
    int lParam,   // INT
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpString,   // LPWSTR out
    int cchSize   // INT
);
<DllImport("USER32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetKeyNameTextW(
    lParam As Integer,   ' INT
    <MarshalAs(UnmanagedType.LPWStr)> lpString As System.Text.StringBuilder,   ' LPWSTR out
    cchSize As Integer   ' INT
) As Integer
End Function
' lParam : INT
' lpString : LPWSTR out
' cchSize : INT
Declare PtrSafe Function GetKeyNameTextW Lib "user32" ( _
    ByVal lParam As Long, _
    ByVal lpString As LongPtr, _
    ByVal cchSize As Long) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetKeyNameTextW = ctypes.windll.user32.GetKeyNameTextW
GetKeyNameTextW.restype = ctypes.c_int
GetKeyNameTextW.argtypes = [
    ctypes.c_int,  # lParam : INT
    wintypes.LPWSTR,  # lpString : LPWSTR out
    ctypes.c_int,  # cchSize : INT
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
GetKeyNameTextW = Fiddle::Function.new(
  lib['GetKeyNameTextW'],
  [
    Fiddle::TYPE_INT,  # lParam : INT
    Fiddle::TYPE_VOIDP,  # lpString : LPWSTR out
    Fiddle::TYPE_INT,  # cchSize : INT
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "user32")]
extern "system" {
    fn GetKeyNameTextW(
        lParam: i32,  // INT
        lpString: *mut u16,  // LPWSTR out
        cchSize: i32  // INT
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("USER32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetKeyNameTextW(int lParam, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpString, int cchSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_GetKeyNameTextW' -Namespace Win32 -PassThru
# $api::GetKeyNameTextW(lParam, lpString, cchSize)
#uselib "USER32.dll"
#func global GetKeyNameTextW "GetKeyNameTextW" wptr, wptr, wptr
; GetKeyNameTextW lParam, varptr(lpString), cchSize   ; 戻り値は stat
; lParam : INT -> "wptr"
; lpString : LPWSTR out -> "wptr"
; cchSize : INT -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "USER32.dll"
#cfunc global GetKeyNameTextW "GetKeyNameTextW" int, var, int
; res = GetKeyNameTextW(lParam, lpString, cchSize)
; lParam : INT -> "int"
; lpString : LPWSTR out -> "var"
; cchSize : INT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; INT GetKeyNameTextW(INT lParam, LPWSTR lpString, INT cchSize)
#uselib "USER32.dll"
#cfunc global GetKeyNameTextW "GetKeyNameTextW" int, var, int
; res = GetKeyNameTextW(lParam, lpString, cchSize)
; lParam : INT -> "int"
; lpString : LPWSTR out -> "var"
; cchSize : INT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procGetKeyNameTextW = user32.NewProc("GetKeyNameTextW")
)

// lParam (INT), lpString (LPWSTR out), cchSize (INT)
r1, _, err := procGetKeyNameTextW.Call(
	uintptr(lParam),
	uintptr(lpString),
	uintptr(cchSize),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // INT
function GetKeyNameTextW(
  lParam: Integer;   // INT
  lpString: PWideChar;   // LPWSTR out
  cchSize: Integer   // INT
): Integer; stdcall;
  external 'USER32.dll' name 'GetKeyNameTextW';
result := DllCall("USER32\GetKeyNameTextW"
    , "Int", lParam   ; INT
    , "Ptr", lpString   ; LPWSTR out
    , "Int", cchSize   ; INT
    , "Int")   ; return: INT
●GetKeyNameTextW(lParam, lpString, cchSize) = DLL("USER32.dll", "int GetKeyNameTextW(int, char*, int)")
# 呼び出し: GetKeyNameTextW(lParam, lpString, cchSize)
# lParam : INT -> "int"
# lpString : LPWSTR out -> "char*"
# cchSize : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。