简体   繁体   中英

Windows Constants for Ctrl+X, Ctrl+C, and Ctrl+V

I've got some older MFC code I wrote that I'm "freshening up" a bit. I have the following code in a window class' OnChar() handler.

I really don't like using constants like 0x18. I'd like to make the code more readable. I know I can declare my own, but are there no Windows macros for these values? I couldn't find anything about this on the web.

// Check for clipboard commands
switch (nChar)
{
    case 0x18: // Ctrl+X - Cut
        OnEditCut();
        break;
    case 0x03: // Ctrl+C - Copy
        OnEditCopy();
        break;
    case 0x16: // Ctrl+V - Paste
        OnEditPaste();
        break;
}

Do you have some code above there which is subtracting an offset from nChar?

Those values are the letters' places in the alphabet, but I don't think character codes normally work like that. (It has been a long time since I used any of this so maybe I'm just mis-remembering.)

Anyway, the code fragment you have is effectively this (at least on architectures that use the ASCII character ordering, ie alphabetic):

// Check for clipboard commands
switch (nChar)
{
    case ('X' - 'A' + 1): // Ctrl+X - Cut
        OnEditCut();
        break;
    case ('C' - 'A' + 1): // Ctrl+C - Copy
        OnEditCopy();
        break;
    case ('V' - 'A' + 1): // Ctrl+V - Paste
        OnEditPaste();
        break;
}

As mentioned in my other comment, I'd expect there to be some other code checking for Ctrl being held down.

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