Question

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;
}
Was it helpful?

Solution

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, i.e. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top