简体   繁体   中英

How to get keyboard accelerators for a command ID?

The MFC feature pack seem to magically print the resources keyboard accelerator shortcuts to the tooltip of a menu item. How would I find the key combination for any given command ID (if all I have is the HACCEL handle?).

You can use the CopyAcceleratorTable() function to retrieve the list of accelerators for a given HACCEL handle.

First, call that function with nullptr and 0 as the second and third arguments to get the size of the list (ie the number of accelerators) and then allocate a buffer of ACCEL structures of appropriate size.

Then you can call CopyAcceleratorTable again with a pointer to that buffer and the previously returned count.

Now you can iterate through that buffer, using the three fields of each structure to determine what the accelerator key is, what flags it has and what command it represents.

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!

You can see a description of the values and formats of the three data fields of the ACCEL structure on this page .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM