RegisterHotKey
関数シグネチャ
// USER32.dll
#include <windows.h>
BOOL RegisterHotKey(
HWND hWnd, // optional
INT id,
HOT_KEY_MODIFIERS fsModifiers,
DWORD vk
);パラメーター
| 名前 | 型 | 方向 | 説明 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| hWnd | HWND | inoptional | ホットキーによって生成される WM_HOTKEY メッセージを受け取るウィンドウへのハンドルです。このパラメーターが NULL の場合、WM_HOTKEY メッセージは呼び出し元スレッドのメッセージキューにポストされ、メッセージループ内で処理する必要があります。 | ||||||||||||
| id | INT | in | ホットキーの識別子です。hWnd パラメーターが NULL の場合、ホットキーは特定のウィンドウではなく現在のスレッドに関連付けられます。同じ hWnd および id パラメーターを持つホットキーが既に存在する場合の動作については、「解説」を参照してください。 | ||||||||||||
| fsModifiers | HOT_KEY_MODIFIERS | in | WM_HOTKEY メッセージを生成するために、vk パラメーターで指定されたキーと組み合わせて押す必要があるキーです。fsModifiers パラメーターには、次の値の組み合わせを指定できます。
| ||||||||||||
| vk | DWORD | in | ホットキーの仮想キーコードです。Virtual Key Codes を参照してください。 |
戻り値の型: BOOL
公式ドキュメント
システム全体で有効なホットキーを定義します。
戻り値
型: BOOL
関数が成功すると、戻り値は 0 以外になります。
関数が失敗すると、戻り値は 0 になります。拡張エラー情報を取得するには、GetLastError を呼び出します。
この関数は、別のスレッドによって作成されたウィンドウにホットキーを関連付けようとすると失敗します。
通常、RegisterHotKey は、ホットキーに指定したキーストロークが別のホットキーとして既に登録されている場合にも失敗します。ただし、OS によって登録された既存の既定のホットキー (Snipping ツールを起動する PrintScreen など) の一部は、アプリのウィンドウのいずれかがフォアグラウンドにあるときに、別のホットキー登録によって上書きされる場合があります。
解説(Remarks)
キーが押されると、システムはすべてのホットキーと照合を行います。一致するものが見つかると、システムはそのホットキーが関連付けられているウィンドウのメッセージキューに WM_HOTKEY メッセージをポストします。ホットキーがウィンドウに関連付けられていない場合、WM_HOTKEY メッセージはそのホットキーに関連付けられたスレッドにポストされます。
同じ hWnd および id パラメーターを持つホットキーが既に存在する場合、そのホットキーは新しいホットキーとともに保持されます。古いホットキーの登録を解除するには、アプリケーションが明示的に UnregisterHotKey を呼び出す必要があります。
F12 キーは常にデバッガーによる使用のために予約されているため、ホットキーとして登録しないでください。アプリケーションをデバッグしていない場合でも、カーネルモードデバッガーやジャストインタイムデバッガーが常駐している場合に備えて、F12 は予約されています。
アプリケーションは、0x0000 から 0xBFFF の範囲の id 値を指定する必要があります。共有 DLL は、0xC000 から 0xFFFF の範囲 (GlobalAddAtom 関数が返す範囲) の値を指定する必要があります。他の共有 DLL によって定義されたホットキー識別子との競合を避けるため、DLL は GlobalAddAtom 関数を使用してホットキー識別子を取得する必要があります。
**Windows Server 2003: **同じ hWnd および id パラメーターを持つホットキーが既に存在する場合、そのホットキーは新しいホットキーに置き換えられます。
例
次の例は、MOD_NOREPEAT フラグを指定して RegisterHotKey 関数を使用する方法を示します。
この例では、ホットキー 'ALT+b' をメインスレッドに登録しています。ホットキーが押されると、スレッドは WM_HOTKEY メッセージを受け取り、それが GetMessage の呼び出しで取得されます。この例では fsModifiers に MOD_ALT と MOD_NOREPEAT の値を使用しているため、スレッドは 'ALT' キーを押し下げたまま 'b' キーを離してから再度押したときにのみ、次の WM_HOTKEY メッセージを受け取ります。
#include "stdafx.h"
int _cdecl _tmain (
int argc,
TCHAR *argv[])
{
if (RegisterHotKey(
NULL,
1,
MOD_ALT | MOD_NOREPEAT,
0x42)) //0x42 is 'b'
{
_tprintf(_T("Hotkey 'ALT+b' registered, using MOD_NOREPEAT flag\n"));
}
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0) != 0)
{
if (msg.message == WM_HOTKEY)
{
_tprintf(_T("WM_HOTKEY received\n"));
}
}
return 0;
}
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// USER32.dll
#include <windows.h>
BOOL RegisterHotKey(
HWND hWnd, // optional
INT id,
HOT_KEY_MODIFIERS fsModifiers,
DWORD vk
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool RegisterHotKey(
IntPtr hWnd, // HWND optional
int id, // INT
uint fsModifiers, // HOT_KEY_MODIFIERS
uint vk // DWORD
);<DllImport("USER32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function RegisterHotKey(
hWnd As IntPtr, ' HWND optional
id As Integer, ' INT
fsModifiers As UInteger, ' HOT_KEY_MODIFIERS
vk As UInteger ' DWORD
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' hWnd : HWND optional
' id : INT
' fsModifiers : HOT_KEY_MODIFIERS
' vk : DWORD
Declare PtrSafe Function RegisterHotKey Lib "user32" ( _
ByVal hWnd As LongPtr, _
ByVal id As Long, _
ByVal fsModifiers As Long, _
ByVal vk As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
RegisterHotKey = ctypes.windll.user32.RegisterHotKey
RegisterHotKey.restype = wintypes.BOOL
RegisterHotKey.argtypes = [
wintypes.HANDLE, # hWnd : HWND optional
ctypes.c_int, # id : INT
wintypes.DWORD, # fsModifiers : HOT_KEY_MODIFIERS
wintypes.DWORD, # vk : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('USER32.dll')
RegisterHotKey = Fiddle::Function.new(
lib['RegisterHotKey'],
[
Fiddle::TYPE_VOIDP, # hWnd : HWND optional
Fiddle::TYPE_INT, # id : INT
-Fiddle::TYPE_INT, # fsModifiers : HOT_KEY_MODIFIERS
-Fiddle::TYPE_INT, # vk : DWORD
],
Fiddle::TYPE_INT)#[link(name = "user32")]
extern "system" {
fn RegisterHotKey(
hWnd: *mut core::ffi::c_void, // HWND optional
id: i32, // INT
fsModifiers: u32, // HOT_KEY_MODIFIERS
vk: u32 // DWORD
) -> 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 RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_RegisterHotKey' -Namespace Win32 -PassThru
# $api::RegisterHotKey(hWnd, id, fsModifiers, vk)#uselib "USER32.dll"
#func global RegisterHotKey "RegisterHotKey" sptr, sptr, sptr, sptr
; RegisterHotKey hWnd, id, fsModifiers, vk ; 戻り値は stat
; hWnd : HWND optional -> "sptr"
; id : INT -> "sptr"
; fsModifiers : HOT_KEY_MODIFIERS -> "sptr"
; vk : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "USER32.dll"
#cfunc global RegisterHotKey "RegisterHotKey" sptr, int, int, int
; res = RegisterHotKey(hWnd, id, fsModifiers, vk)
; hWnd : HWND optional -> "sptr"
; id : INT -> "int"
; fsModifiers : HOT_KEY_MODIFIERS -> "int"
; vk : DWORD -> "int"; BOOL RegisterHotKey(HWND hWnd, INT id, HOT_KEY_MODIFIERS fsModifiers, DWORD vk)
#uselib "USER32.dll"
#cfunc global RegisterHotKey "RegisterHotKey" intptr, int, int, int
; res = RegisterHotKey(hWnd, id, fsModifiers, vk)
; hWnd : HWND optional -> "intptr"
; id : INT -> "int"
; fsModifiers : HOT_KEY_MODIFIERS -> "int"
; vk : DWORD -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
user32 = windows.NewLazySystemDLL("USER32.dll")
procRegisterHotKey = user32.NewProc("RegisterHotKey")
)
// hWnd (HWND optional), id (INT), fsModifiers (HOT_KEY_MODIFIERS), vk (DWORD)
r1, _, err := procRegisterHotKey.Call(
uintptr(hWnd),
uintptr(id),
uintptr(fsModifiers),
uintptr(vk),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction RegisterHotKey(
hWnd: THandle; // HWND optional
id: Integer; // INT
fsModifiers: DWORD; // HOT_KEY_MODIFIERS
vk: DWORD // DWORD
): BOOL; stdcall;
external 'USER32.dll' name 'RegisterHotKey';result := DllCall("USER32\RegisterHotKey"
, "Ptr", hWnd ; HWND optional
, "Int", id ; INT
, "UInt", fsModifiers ; HOT_KEY_MODIFIERS
, "UInt", vk ; DWORD
, "Int") ; return: BOOL●RegisterHotKey(hWnd, id, fsModifiers, vk) = DLL("USER32.dll", "bool RegisterHotKey(void*, int, dword, dword)")
# 呼び出し: RegisterHotKey(hWnd, id, fsModifiers, vk)
# hWnd : HWND optional -> "void*"
# id : INT -> "int"
# fsModifiers : HOT_KEY_MODIFIERS -> "dword"
# vk : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "user32" fn RegisterHotKey(
hWnd: ?*anyopaque, // HWND optional
id: i32, // INT
fsModifiers: u32, // HOT_KEY_MODIFIERS
vk: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;proc RegisterHotKey(
hWnd: pointer, # HWND optional
id: int32, # INT
fsModifiers: uint32, # HOT_KEY_MODIFIERS
vk: uint32 # DWORD
): int32 {.importc: "RegisterHotKey", stdcall, dynlib: "USER32.dll".}pragma(lib, "user32");
extern(Windows)
int RegisterHotKey(
void* hWnd, // HWND optional
int id, // INT
uint fsModifiers, // HOT_KEY_MODIFIERS
uint vk // DWORD
);ccall((:RegisterHotKey, "USER32.dll"), stdcall, Int32,
(Ptr{Cvoid}, Int32, UInt32, UInt32),
hWnd, id, fsModifiers, vk)
# hWnd : HWND optional -> Ptr{Cvoid}
# id : INT -> Int32
# fsModifiers : HOT_KEY_MODIFIERS -> UInt32
# vk : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t RegisterHotKey(
void* hWnd,
int32_t id,
uint32_t fsModifiers,
uint32_t vk);
]]
local user32 = ffi.load("user32")
-- user32.RegisterHotKey(hWnd, id, fsModifiers, vk)
-- hWnd : HWND optional
-- id : INT
-- fsModifiers : HOT_KEY_MODIFIERS
-- vk : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('USER32.dll');
const RegisterHotKey = lib.func('__stdcall', 'RegisterHotKey', 'int32_t', ['void *', 'int32_t', 'uint32_t', 'uint32_t']);
// RegisterHotKey(hWnd, id, fsModifiers, vk)
// hWnd : HWND optional -> 'void *'
// id : INT -> 'int32_t'
// fsModifiers : HOT_KEY_MODIFIERS -> 'uint32_t'
// vk : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("USER32.dll", {
RegisterHotKey: { parameters: ["pointer", "i32", "u32", "u32"], result: "i32" },
});
// lib.symbols.RegisterHotKey(hWnd, id, fsModifiers, vk)
// hWnd : HWND optional -> "pointer"
// id : INT -> "i32"
// fsModifiers : HOT_KEY_MODIFIERS -> "u32"
// vk : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t RegisterHotKey(
void* hWnd,
int32_t id,
uint32_t fsModifiers,
uint32_t vk);
C, "USER32.dll");
// $ffi->RegisterHotKey(hWnd, id, fsModifiers, vk);
// hWnd : HWND optional
// id : INT
// fsModifiers : HOT_KEY_MODIFIERS
// vk : DWORD
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface User32 extends StdCallLibrary {
User32 INSTANCE = Native.load("user32", User32.class);
boolean RegisterHotKey(
Pointer hWnd, // HWND optional
int id, // INT
int fsModifiers, // HOT_KEY_MODIFIERS
int vk // DWORD
);
}@[Link("user32")]
lib LibUSER32
fun RegisterHotKey = RegisterHotKey(
hWnd : Void*, # HWND optional
id : Int32, # INT
fsModifiers : UInt32, # HOT_KEY_MODIFIERS
vk : UInt32 # DWORD
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef RegisterHotKeyNative = Int32 Function(Pointer<Void>, Int32, Uint32, Uint32);
typedef RegisterHotKeyDart = int Function(Pointer<Void>, int, int, int);
final RegisterHotKey = DynamicLibrary.open('USER32.dll')
.lookupFunction<RegisterHotKeyNative, RegisterHotKeyDart>('RegisterHotKey');
// hWnd : HWND optional -> Pointer<Void>
// id : INT -> Int32
// fsModifiers : HOT_KEY_MODIFIERS -> Uint32
// vk : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function RegisterHotKey(
hWnd: THandle; // HWND optional
id: Integer; // INT
fsModifiers: DWORD; // HOT_KEY_MODIFIERS
vk: DWORD // DWORD
): BOOL; stdcall;
external 'USER32.dll' name 'RegisterHotKey';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "RegisterHotKey"
c_RegisterHotKey :: Ptr () -> Int32 -> Word32 -> Word32 -> IO CInt
-- hWnd : HWND optional -> Ptr ()
-- id : INT -> Int32
-- fsModifiers : HOT_KEY_MODIFIERS -> Word32
-- vk : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let registerhotkey =
foreign "RegisterHotKey"
((ptr void) @-> int32_t @-> uint32_t @-> uint32_t @-> returning int32_t)
(* hWnd : HWND optional -> (ptr void) *)
(* id : INT -> int32_t *)
(* fsModifiers : HOT_KEY_MODIFIERS -> uint32_t *)
(* vk : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)
(cffi:defcfun ("RegisterHotKey" register-hot-key :convention :stdcall) :int32
(h-wnd :pointer) ; HWND optional
(id :int32) ; INT
(fs-modifiers :uint32) ; HOT_KEY_MODIFIERS
(vk :uint32)) ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $RegisterHotKey = Win32::API::More->new('USER32',
'BOOL RegisterHotKey(HANDLE hWnd, int id, DWORD fsModifiers, DWORD vk)');
# my $ret = $RegisterHotKey->Call($hWnd, $id, $fsModifiers, $vk);
# hWnd : HWND optional -> HANDLE
# id : INT -> int
# fsModifiers : HOT_KEY_MODIFIERS -> DWORD
# vk : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f GlobalAddAtomW — 文字列をグローバルアトムテーブルに追加しアトムを返す(Unicode)。
- f UnregisterHotKey — 登録済みのホットキーを解除する。