locked
Capturing keystrokes in child controls RRS feed

  • Question

  • Hi

    I have made a program in C with Visual C++ 2010 Express, and I need to capture keystrokes when child controls have focus. As I have understood, the way to do this is by what is called subclassing. I have tried to implement a solution given by Petzold in his book, chapter 9, but can’t make it work.

    First I have changed the message loop in Winmain into the following:

     while( GetMessage(&msg, NULL, 0, 0)) {
        
    	if(!IsDialogMessage(hwnd2,&msg)) 
        {                                    
           TranslateMessage(&msg);      
           DispatchMessage(&msg);
        }  
    	  

    I have read that this is necessary to make it possible to change focus by using the Tab key. Then I have the following callback function:

    LRESULT CALLBACK KeyCatching (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    	
    {
    if(message==WM_KEYDOWN) tescounter++; 
    return CallWindowProc (KeyControl[0], hwnd, message, wParam, lParam) ;
    }

    I am for now only using a counter to check if I am able to capture any keystrokes at all. The rest I guess I can handle if I only can get it to work. Then I am calling this funtion in the WM_CREATE section in WndProc:

    Control[0] = CreateWindow(TEXT("combobox"), NULL, 
                      WS_CHILD | WS_VISIBLE | WS_TABSTOP | CBS_DROPDOWN, 
                      0, 0, 0, 0, hwnd, NULL, g_hinst, NULL);  
    
    		SendMessage(Control[0], CB_ADDSTRING, 0, (LPARAM) comboboxkeynoteitems[0]);
    		SendMessage(Control[0], CB_ADDSTRING, 0, (LPARAM) comboboxkeynoteitems[1]);
    		SendMessage(Control[0], CB_ADDSTRING, 0, (LPARAM) comboboxkeynoteitems[2]);
    		SendMessage(Control[0], CB_ADDSTRING, 0, (LPARAM) comboboxkeynoteitems[3]);
    		SendMessage(Control[0], CB_ADDSTRING, 0, (LPARAM) comboboxkeynoteitems[4]);
    		SendMessage(Control[0], CB_ADDSTRING, 0, (LPARAM) comboboxkeynoteitems[5]);
    		SendMessage(Control[0], CB_ADDSTRING, 0, (LPARAM) comboboxkeynoteitems[6]);
    		SendMessage(Control[0], CB_ADDSTRING, 0, (LPARAM) comboboxkeynoteitems[7]);
    		SendMessage(Control[0], CB_ADDSTRING, 0, (LPARAM) comboboxkeynoteitems[8]);
    		SendMessage(Control[0], CB_ADDSTRING, 0, (LPARAM) comboboxkeynoteitems[9]);
    		SendMessage(Control[0], CB_ADDSTRING, 0, (LPARAM) comboboxkeynoteitems[10]);
    		SendMessage(Control[0], CB_ADDSTRING, 0, (LPARAM) comboboxkeynoteitems[11]);
    		
    		SendMessage(Control[0], CB_SETCURSEL, 0, 0); 
    
    
    KeyControl[0] = (WNDPROC) SetWindowLongPtr (Control[0], GWL_WNDPROC, (LONG) KeyCatching) ;
    

    Finally I have KeyControl as a global variable:

    WNDPROC KeyControl[1];

    I guess I then would have to make one instance of the call to the KeyCatching function for each of the controls that I want to capture keystrokes from.

    However, this doesn' work, and I have come to the end of my reasoning and am stuck. Greatly appreciated if someone could help me with this.

    Thanks in advance.

    Sincerely

    Monday, July 24, 2017 1:27 PM

Answers

  • Okay. I'll do that.

    Sincerely

    • Marked as answer by Gurunama Tuesday, July 25, 2017 8:18 PM
    Tuesday, July 25, 2017 8:18 PM

All replies

  • WM_KEYDOWN is reveived by the Edit control  of a CBS_DROPDOWN Combo Box

    (the Combo Box receives Edit control notifications such EN_CHANGE in WM_COMMAND)

    And if you want to subclass many Combo Boxes, use Superclassing instead (GetClassInfo() + RegisterClass())

    • Proposed as answer by Pintu Shukla Tuesday, July 25, 2017 4:57 PM
    Monday, July 24, 2017 8:56 PM
  • Thank you very much. That solved it. Now works.

    Sincerely

    Tuesday, July 25, 2017 12:03 PM
  • Since your problem has been resolved, please mark the thread answered.
    Tuesday, July 25, 2017 1:41 PM
  • Actually, there is one more issue related to this. The reason why I wanted to be able to catch keypresses in the child controls is that I want them to bring the focus back to the main window. So that pressing for instance a character key when a child control has the focus, the focus is automatically brought back to the main window. Is there any way to do this?

    Thanks in advance.

    Sincerely

    Tuesday, July 25, 2017 3:31 PM
  • Actually, there is one more issue related to this. The reason why I wanted to be able to catch keypresses in the child controls is that I want them to bring the focus back to the main window. So that pressing for instance a character key when a child control has the focus, the focus is automatically brought back to the main window. Is there any way to do this?


    This is a different question.  It would be appropriate to mark the answer to close this thread and start a new thread for your next question.
    Tuesday, July 25, 2017 3:51 PM
  • Okay. I'll do that.

    Sincerely

    • Marked as answer by Gurunama Tuesday, July 25, 2017 8:18 PM
    Tuesday, July 25, 2017 8:18 PM