将VK_RETURN VK_DELETE等按键消息转换为WM_CHAR消息

本文介绍了一种将特定按键事件如VK_RETURN、VK_DELETE等转换为WM_CHAR消息的方法,并通过响应WM_KEYDOWN消息实现。文中还解释了如何使用GetKeyState(VK_CONTROL)来检测CTRL键的按下状态,同时提供了区分不同虚拟键状态的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

并不是所有的按键事件都会转换为WM_CHAR消息,下面是将VK_RETURN VK_DELETE等按键消息转换为WM_CHAR

消息的方法。 

响应WM_KEYDOWN消息

if( pMsg->message == WM_KEYDOWN )
 {
  //vk_retun 和 vk_esc按键成为WM_CHAR消息的方法。
  if(pMsg->wParam == VK_RETURN
    || pMsg->wParam == VK_DELETE
    || pMsg->wParam == VK_ESCAPE
    || GetKeyState( VK_CONTROL)//单纯检测CTRL的按下状况,不区分左右。
    )
  {
   ::TranslateMessage(pMsg);//made VK_RETURN to  CHAR message.....
   ::DispatchMessage(pMsg);
   return TRUE;       // DO NOT process further
  }
 }

 

下面是解释使用GetKeyState( VK_CONTROL)的好处。

An application can use the virtual-key code constants VK_SHIFT, VK_CONTROL, and VK_MENU as values for the nVirtKey parameter. This gives the status of the SHIFT, CTRL, or ALT keys without distinguishing between left and right. An application can also use the following virtual-key code constants as values for nVirtKey to distinguish between the left and right instances of those keys:

VK_LSHIFTVK_RSHIFT
VK_LCONTROLVK_RCONTROL
VK_LMENUVK_RMENU
下面是一个简单的英文文本编辑器的代码,其中实现了基本功能和部分附加功能:Ctrl+A实现选中全部文字并变蓝色,Ctrl+C和Ctrl+V实现复制和粘贴功能,Ctrl+S实现保存到文件,R、G、B分别实现文字颜色的变化,方向键实现光标的移动。 ```cpp #include <windows.h> #include <string> #include <fstream> LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); const int ID_EDIT = 1; bool ctrlPressed = false; bool rPressed = false; bool gPressed = false; bool bPressed = false; COLORREF textColor = RGB(0, 0, 0); COLORREF bgColor = RGB(255, 255, 255); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { const char* CLASS_NAME = "SimpleTextEditor"; WNDCLASS wc = {}; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME; RegisterClass(&wc); HWND hWnd = CreateWindowEx( 0, CLASS_NAME, "Simple Text Editor", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, hInstance, nullptr ); MSG msg = {}; while (GetMessage(&msg, nullptr, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { static HWND hEdit; switch (message) { case WM_CREATE: hEdit = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL, 0, 0, 0, 0, hWnd, (HMENU)ID_EDIT, GetModuleHandle(nullptr), nullptr ); break; case WM_SIZE: MoveWindow(hEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE); break; case WM_COMMAND: if (LOWORD(wParam) == ID_EDIT) { if (HIWORD(wParam) == EN_UPDATE) { // text has been updated } } break; case WM_KEYDOWN: if (wParam == VK_CONTROL) { ctrlPressed = true; } else if (ctrlPressed) { if (wParam == 'A') { SendMessage(hEdit, EM_SETSEL, 0, -1); SendMessage(hEdit, EM_SETBKGNDCOLOR, 0, bgColor); SendMessage(hEdit, EM_SETTEXTCOLOR, 0, RGB(0, 0, 255)); } else if (wParam == 'C') { SendMessage(hEdit, WM_COPY, 0, 0); } else if (wParam == 'V') { SendMessage(hEdit, WM_PASTE, 0, 0); } else if (wParam == 'S') { char buffer[MAX_PATH]; OPENFILENAME ofn = {}; ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hWnd; ofn.lpstrFile = buffer; ofn.nMaxFile = MAX_PATH; ofn.Flags = OFN_OVERWRITEPROMPT; if (GetSaveFileName(&ofn)) { std::ofstream file(ofn.lpstrFile); int length = GetWindowTextLength(hEdit); char* text = new char[length + 1]; GetWindowText(hEdit, text, length + 1); file << text; delete[] text; } } else if (wParam == 'R') { rPressed = true; gPressed = false; bPressed = false; textColor = RGB(255, 0, 0); SendMessage(hEdit, EM_SETBKGNDCOLOR, 0, bgColor); SendMessage(hEdit, EM_SETTEXTCOLOR, 0, textColor); } else if (wParam == 'G') { rPressed = false; gPressed = true; bPressed = false; textColor = RGB(0, 255, 0); SendMessage(hEdit, EM_SETBKGNDCOLOR, 0, bgColor); SendMessage(hEdit, EM_SETTEXTCOLOR, 0, textColor); } else if (wParam == 'B') { rPressed = false; gPressed = false; bPressed = true; textColor = RGB(0, 0, 255); SendMessage(hEdit, EM_SETBKGNDCOLOR, 0, bgColor); SendMessage(hEdit, EM_SETTEXTCOLOR, 0, textColor); } } else if (wParam == VK_LEFT) { int pos = SendMessage(hEdit, EM_GETSEL, 0, 0); SendMessage(hEdit, EM_SETSEL, pos - 1, pos - 1); } else if (wParam == VK_RIGHT) { int pos = SendMessage(hEdit, EM_GETSEL, 0, 0); SendMessage(hEdit, EM_SETSEL, pos + 1, pos + 1); } else if (wParam == VK_UP) { int line = SendMessage(hEdit, EM_LINEFROMCHAR, -1, 0); int pos = SendMessage(hEdit, EM_LINEINDEX, line - 1, 0); SendMessage(hEdit, EM_SETSEL, pos, pos); } else if (wParam == VK_DOWN) { int line = SendMessage(hEdit, EM_LINEFROMCHAR, -1, 0); int pos = SendMessage(hEdit, EM_LINEINDEX, line + 1, 0); SendMessage(hEdit, EM_SETSEL, pos, pos); } break; case WM_KEYUP: if (wParam == VK_CONTROL) { ctrlPressed = false; } else if (wParam == 'R') { if (!gPressed && !bPressed) { textColor = RGB(0, 0, 0); SendMessage(hEdit, EM_SETBKGNDCOLOR, 0, bgColor); SendMessage(hEdit, EM_SETTEXTCOLOR, 0, textColor); } rPressed = false; } else if (wParam == 'G') { if (!rPressed && !bPressed) { textColor = RGB(0, 0, 0); SendMessage(hEdit, EM_SETBKGNDCOLOR, 0, bgColor); SendMessage(hEdit, EM_SETTEXTCOLOR, 0, textColor); } gPressed = false; } else if (wParam == 'B') { if (!rPressed && !gPressed) { textColor = RGB(0, 0, 0); SendMessage(hEdit, EM_SETBKGNDCOLOR, 0, bgColor); SendMessage(hEdit, EM_SETTEXTCOLOR, 0, textColor); } bPressed = false; } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值