主题
注册键盘快捷键 - RegisterHotkey
函数简介
注册键盘快捷键监听,可监听单个按键、组合键等,同一组按键只能创建一个监听。
接口名称
RegisterHotkeyDLL调用
int RegisterHotkey(long ola, int keycode, int modifiers, HotKeyCallback callback);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| ola | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| keycode | 整数型 | 按键码。传0可监听所有按键信息。 |
| modifiers | 整数型 | 修饰键组合,使用位或组合,如 Ctrl+Alt 为 2+8=10。取值:无组合键(0)、左Shift(1)、左Ctrl(2)、左Meta(4)、左Alt(8)、右Shift(16)、右Ctrl(32)、右Meta(64)、右Alt(128)。 |
| callback | 回调函数 | 回调函数 int HotKeyCallback(int keycode, int modifiers),返回0继续传递按键信息,返回1阻断消息传递。 |
示例
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);
}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 安装全局钩子
- 程序退出前应 UnregisterHotkey 并 StopHotkeyHook
- 回调返回值类型是 int:返回 0 继续传递按键,返回 1 阻断消息
- 同一组 keycode + modifiers 只能注册一个监听
- 参考 Windows 函数 SetWindowsHookExW 实现
