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