Adi Shavit
Adi Shavit

Reputation: 17265

Handling Ctrl+C with Win32

I have a Win32 window message loop. I want to intercept "Copy to clipboard" via CTRL+C.
My current approach is to handle it like this:

...
case WM_KEYDOWN:
   TranslateMessage(&message);

   // Intercept Ctrl+C for copy to clipboard
   if ('C' == message.wParam && (::GetKeyState(VK_CONTROL)>>15))
   { // do the copy... }
...

Is there a better way to do this other than explicitly checking for the key-stroke combination?

Is there some way to register the standard copy-to-clipboard keystroke and then handle a WM_COPY message?

Upvotes: 6

Views: 2119

Answers (1)

user13947194
user13947194

Reputation: 402

Windows treats Ctrl+C, Ctrl+V, Ctrl+X as one key in the WM_CHAR message.

enum
{
 CTRL_BASE  = 'A' - 1,
 SELECT_ALL = 'A' - CTRL_BASE, // Ctrl+A
 COPY       = 'C' - CTRL_BASE, // Ctrl+C
 CUT        = 'X' - CTRL_BASE, // Ctrl+X
 PASTE      = 'V' - CTRL_BASE, // Ctrl+V
 UNDO       = 'Z' - CTRL_BASE, // Ctrl+Z
 REDO       = 'Y' - CTRL_BASE, // Ctrl+Y
};

Note: This is not documented at MSDN - WM_CHAR message. This is an observation I made while creating my text editor. Although Ctrl+A == 0x41 is mentioned in Keyboard Input.

Using WM_CHAR instead of manually handling WM_KEYDOWN, etc makes processing closer to the standard, specifically does auto-repeat when a key is held, and does not emit the message when extra keys are held.

Apparently there's no readily available way to have WM_COPY delivered for you instead of keys.

Upvotes: 2

Related Questions