Basj
Basj

Reputation: 46163

Detect CTRL+A in a texbox

I have a main window hwndMain and a multiline Edit textbox:

hwndEdit = CreateWindowEx(0, L"EDIT", NULL, WS_CHILD | WS_VISIBLE | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL, 0, 0, 300, 200, hwndMain, 0, (HINSTANCE) GetWindowLong(hwndMain, GWL_HINSTANCE), NULL);

I use this to detect CTRL+A in the textbox (because strangely, it's not available out-of-the-box):

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_KEYDOWN:
        {
            if (wParam == VK_CONTROL)  // something here missing for detecting "A"
            {
                SendMessage(hwndEdit, EM_SETSEL, 0, -1);    // select all ; this code works on its own, tested
            }
            ...

Unfortunately, nothing happens when I do CTRL+A or CTRL+B or CTRL+ anything.

What is wrong?

Note: Ok the code for detecting A is still missing (i still don't know how to do it), but the code here should work for any CTRL+key...

Upvotes: 0

Views: 186

Answers (2)

After you create all your windows:

hwndEdit = CreateWindowEx(.....)
...

//Subbclassing
SetWindowSubclass(hwndEdit, (SUBCLASSPROC)EditWndProc, 0, 1);


LRESULT CALLBACK EditWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwrefData){

    switch(message){ //handle the messages   
        case WM_KEYDOWN:
            //your code

           break;
        default:   //for messages that we don't deal with
            return DefSubclassProc(hwnd, message, wParam, lParam);
    }

    return DefSubclassProc(hwnd, message, wParam, lParam);
}

Upvotes: 1

xMRi
xMRi

Reputation: 15375

Just check for WM_KEYDOWN for A and than use GetKeyState

case WM_KEYDOWN:
  {
    if (wParam=='A' && (::GetKeyState(VK_CONTROL) & 0x8000)!=0)
    {
      SendMessage(hwndEdit, EM_SETSEL, 0, -1);    // select all
    }

Remember that WM_KEYDOWN is only sent to the window that has the focus and not to parent windows.

Upvotes: 1

Related Questions