如何获取命令ID的键盘快捷键

How to get keyboard accelerators for a command ID?

本文关键字:ID 键盘 快捷键 命令 何获取 获取      更新时间:2024-09-21
MFC功能包似乎神奇地将资源键盘快捷键打印到菜单项的工具提示中。如何找到任何给定命令ID的组合键(如果我只有HACCEL句柄?(。

您可以使用CopyAcceleratorTable()函数来检索给定HACCEL句柄的加速器列表。

首先,用nullptr0作为第二个和第三个参数调用该函数,以获得列表的大小(即加速器的数量(,然后分配适当大小的ACCEL结构的缓冲区。

然后,您可以使用指向该缓冲区的指针和之前返回的计数再次调用CopyAcceleratorTable

现在,您可以遍历该缓冲区,使用每个结构的三个字段来确定加速器键是什么、它有什么标志以及它代表什么命令。

HACCEL hAcc; // Assuming this is a valid handle ...
int nAcc = CopyAcceleratorTable(hAcc, nullptr, 0); // Get number of accelerators
ACCEL *pAccel = new ACCEL[nAcc];                   // Allocate the required buffer
CopyAcceleratorTable(hAcc, pAccel, nAcc);          // Now copy to that buffer
for (int a = 0; a < nAcc; ++a) {
DWORD cmd = pAccel[a].cmd; // The command ID
WORD  key = pAccel[a].key; // The key code - such as 0x41 for the "A" key
WORD  flg = pAccel[a].fVirt; // The 'flags' (things like FCONTROL, FALT, etc)
//...
// Format and display the data, as required
//...
}
delete[] pAccel; // Don't forget to free the data when you're done!

您可以在此页面上看到ACCEL结构的三个数据字段的值和格式的描述。