主题
注册键盘快捷键 - RegisterHotkey
函数简介
注册键盘快捷键监听,可监听单个按键、组合键等,同一组按键只能创建一个监听。
接口名称
RegisterHotkeyDLL调用
int RegisterHotkey(long ola, int keycode, int modifiers, HotKeyCallback callback);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| ola | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| keycode | 整数型 | 按键码。传0可监听所有按键信息。 |
| modifiers | 整数型 | 修饰键组合,使用位或组合,见下表。 |
| callback | 回调函数 | 回调函数 int HotKeyCallback(int keycode, int modifiers),返回0继续传递按键信息,返回1阻断消息传递。 |
modifiers 修饰键
将需要的修饰键 按位或(|)相加 后传入 modifiers 参数:
| 值 | 说明 |
|---|---|
| 0 | 无组合键 |
| 1 | 左 Shift |
| 2 | 左 Ctrl |
| 4 | 左 Meta(Win 键) |
| 8 | 左 Alt |
| 16 | 右 Shift |
| 32 | 右 Ctrl |
| 64 | 右 Meta |
| 128 | 右 Alt |
位运算组合示例
csharp
// Ctrl + Alt + F9:modifiers = 2 + 8 = 10
int modifiers = 2 | 8;
ola.RegisterHotkey(0x78, modifiers, callback); // 0x78 = F9
// Ctrl + Shift + A:modifiers = 2 + 1 = 3
ola.RegisterHotkey(0x41, 2 | 1, callback);cpp
// Ctrl + Alt:2 | 8 = 10
int modifiers = 2 | 8;
ola.RegisterHotkey(120, modifiers, [](int keycode, int modifiers) -> int {
return 0;
});python
# Ctrl + Alt + F9
modifiers = 2 | 8 # 10
ola.RegisterHotkey(0x78, modifiers, callback)示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
int ret = ola.StartHotkeyHook();
if (ret == 1) {
// 注册 Ctrl+Alt+F9,回调返回 1 可阻断按键传递
int hotkeyRet = ola.RegisterHotkey(120, 10, [](int keycode, int modifiers) -> int {
// 在此处理热键逻辑
return 0;
});
}csharp
using OLAPlug;
var ola = new OLAPlugServer();
int ret = ola.StartHotkeyHook();
if (ret == 1)
{
// 注册 Ctrl+Alt+F9
int hotkeyRet = ola.RegisterHotkey(120, 10, (keycode, modifiers) => {
// 在此处理热键逻辑,返回 0 继续传递,返回 1 阻断
return 0;
});
}python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
ret = ola.StartHotkeyHook()
if ret == 1:
def on_hotkey(keycode, modifiers):
# 在此处理热键逻辑,返回 0 继续传递,返回 1 阻断
return 0
hotkey_ret = ola.RegisterHotkey(120, 10, on_hotkey)java
import com.olaplug.OLAPlugServer;
OLAPlugServer ola = new OLAPlugServer();
int ret = ola.StartHotkeyHook();
if (ret == 1) {
// 注册 Ctrl+Alt+F9,按 Java SDK 实现 HotkeyCallback
int hotkeyRet = ola.RegisterHotkey(120, 10, null);
}go
import "github.com/ola/olaplug/olaplug"
ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
ret := ola.StartHotkeyHook()
if ret == 1 {
// 注册 Ctrl+Alt+F9,回调返回 1 可阻断按键传递
hotkeyRet := ola.RegisterHotkey(120, 10, [](int keycode, int modifiers) -> int {
// 在此处理热键逻辑
return 0
})
}rust
use olaplug::OLAPlugServer;
let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let ret = ola.start_hotkey_hook();
if ret == 1 {
// 注册 Ctrl+Alt+F9,回调返回 1 可阻断按键传递
let hotkeyRet = ola.register_hotkey(120, 10, [](int keycode, int modifiers) -> int {
// 在此处理热键逻辑
return 0;
})
}cpp
var ola = com("OlaPlug.OlaSoft")
var ret = ola.StartHotkeyHook()
if(ret == 1) {
var hotkeyRet = ola.RegisterHotkey(120, 10, 0)
}vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
ret = ola.StartHotkeyHook()
If ret = 1 Then
hotkeyRet = ola.RegisterHotkey(120, 10, 0)
End Iftext
.局部变量 ola, OLAPlug
ola.创建 ()
ret = ola.StartHotkeyHook ()
.如果真 (ret = 1)
hotkeyRet = ola.RegisterHotkey (120, 10, 0)
.如果真结束aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var ret = ola.StartHotkeyHook();
if(ret == 1){
var hotkeyRet = ola.RegisterHotkey(120, 10, function(keycode, modifiers){ return 0; });
}text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
整数 ret = ola.StartHotkeyHook()
如果真 (ret = 1)
{
整数 hotkeyRet = ola.RegisterHotkey(120, 10, 0)
}cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
int32_t ret = ola.StartHotkeyHook();
if (ret == 1) {
int32_t hotkeyRet = ola.RegisterHotkey(120, 10, nullptr);
}原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
StartHotkeyHook(instance);
RegisterHotkey(instance, 120, 10, 0);csharp
using System.Runtime.InteropServices;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int StartHotkeyHook(long ola);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int RegisterHotkey(long ola, int keycode, int modifiers, long callback);
long instance = CreateCOLAPlugInterFace();
StartHotkeyHook(instance);
RegisterHotkey(instance, 120, 10, 0);python
from ctypes import CDLL, c_int, c_int64
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
StartHotkeyHook(instance);
RegisterHotkey(instance, 120, 10, 0);返回值
| 返回值 | 说明 |
|---|---|
1 | 成功。 |
0 | 失败。 |
注意事项
| 项目 | 说明 |
|---|---|
| StartHotkeyHook | 注册前必须先调用 StartHotkeyHook 安装全局钩子。 |
| UnregisterHotkey | 程序退出前应 UnregisterHotkey 并 StopHotkeyHook。 |
| 回调返回值类型是 int | 返回 0 继续传递按键,返回 1 阻断消息。 |
| keycode + modifiers | 同一组 keycode + modifiers 只能注册一个监听。 |
| SetWindowsHookExW | 参考 Windows 函数 SetWindowsHookExW 实现。 |
