0

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;
}
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • 1
    Are you sure that code is actually used, and the hotkeys aren't being handled by something else (like an accelerator table)? It looks wrong to me as I'd expect the character codes to be 'X' 'C' and 'V' and for there to be checks on the separate flags argument to see if ctrl is held down. Maybe I've forgotten how OnChar works, though. – Leo Davidson Dec 30 '10 at 07:52
  • There's no question that the code above works as described. – Jonathan Wood Dec 30 '10 at 08:03

1 Answers1

2

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.

Leo Davidson
  • 6,093
  • 1
  • 27
  • 29
  • 1
    `'X'-'@'` it's IMO even better – 6502 Dec 30 '10 at 08:04
  • @6502, fair enough. I find the way I did it clearer as I'm not likely to remember that @ comes before A in the ASCII table, but it's a matter of taste. – Leo Davidson Dec 30 '10 at 08:09
  • 1
    @Leo: What about `#define CTRL(x) ((x)-'@')` ? this way the code becomes `case CTRL('X'):...` – 6502 Dec 30 '10 at 08:13
  • @Leo: Yes, I understand why this seems strange to you--it does to me as well. But my code definitely isn't changing nChar, and I did find a couple of references on the Web using the values I'm using. The code you present seems a little less arbitrary, but probably no more self-documenting. – Jonathan Wood Dec 30 '10 at 08:14
  • @6502: Yes, that would make the code a little more self-documenting. Of course, the macro still seems odd. For that matter, I could just do #define CTRL_X 0x18. I'm not sure it's worth spending more time on. – Jonathan Wood Dec 30 '10 at 08:16
  • @Jonathan: Maybe it's an MFCism which I've forgotten (or never knew :)). I would have expected OnChar to be a thin wrapper around the Win32 WM_CHAR message, where the character code is UTF-16 (so you'd expect 'X' not 'X'-'@' etc.). The OnChar docs are annoyingly vague about what the nChar argument is, though. :( The WM_CHAR docs are better: http://msdn.microsoft.com/en-us/library/ms646276%28v=vs.85%29.aspx – Leo Davidson Dec 30 '10 at 08:23
  • @Leo: I'm really quite sure this is straight from WM_CHAR, unmodified. http://winapi.freetechsecrets.com/win32/WIN32WMCHAR.htm; http://www.codeguru.com/forum/archive/index.php/t-49901.html; and http://stackoverflow.com/questions/1405256/how-to-find-plain-character-code-of-a-key-press. – Jonathan Wood Dec 30 '10 at 15:21