Thread: Edit control in dialog picking up keys too fast

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92

    Edit control in dialog picking up keys too fast

    Hi

    I have a dialog window, and inside that an edit control. But when I try to type something in this control it catches like 10 key presses immidiatly. This is my message handler and the window proc for the dialog box:
    Code:
    PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
    		if(!IsDialogMessage(moveObj.hWnd, &msg))
    			{
    				TranslateMessage(&msg);
    				DispatchMessage(&msg);
    			}
    
    BOOL CALLBACK dlgMoveObjProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	switch(message)
    	{
    	case WM_COMMAND:
    		switch(LOWORD(wParam))
    		{
    			int something = 0; //This is just something that fills the function up
    			break;
    		}
    	break;
    	default:
    		return false;
    	}
    	return true;
    }
    I don't know if this might be caused by some problems in my code or this just is a common problem.

    Thanks in advance

    The Wazaa

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Try using GetMessage rather than PeekMessage.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    Thanks, that works a bit better. Its only that now it counts one keystroke as four characters. But it waits with creating the next four characters a bit

    The Wazaa

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You should put your PeekMessage in an if-statement, otherwise you'll try to handle messages even if there are none:
    Code:
    while(SomeCondition)
    {
      if(PeekMessage(...))
      {
        if(!IsDialogMessage(...))
        {
           TranslateMessage(...);
           DispatchMessage(...);
        }
      }
    }
    GetMessage is blocking, PeekMessage is not. If you have need for some periodical activities go with PeekMessage otherwise go with GetMessage as it will save you CPU cycles (put that in the while instead of the if):
    Code:
    while(GetMessage(...))
    {
      Translate...
      Dispatch...
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    This is for a level editor so I think Ill go with the last one, but the problem that it counts every keystroke as four keys is still there :-/ Might that be caused by the fact that I have four controls in the dialog?

    Thanks a lot for your replies

    The Wazaa

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    When you say 4 keys do you mean WM_COMMAND is sent 4 times? That is not the same as 4 entered characters. Or does the edit control indeed duplicate each character 4 times, cause that would really surprise me...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    I think that it gets four messages, I'm not sure. The edit box get four characters though. I can select three of them with the mouse and delete them, but that is a quite hard way to write

    The Wazaa

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    how 'bout posting the code since the problem rather lies there than in the Windows Core.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    Oh, that might be a good idea
    Code:
    if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    		{
    			if(!IsDialogMessage(moveObj.hWnd, &msg))
    			{
    				TranslateMessage(&msg);
    				DispatchMessage(&msg);
    			}
    
    			if (msg.message == WM_QUIT)
    			{
    				exit(0);
    			}
    			else
    			{
    				TranslateMessage(&msg);
    				DispatchMessage(&msg);
    			}
    		}
    
    //The proc for the dialog:
    
    BOOL CALLBACK dlgMoveObjProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	switch(message)
    	{
    	case WM_COMMAND:
    	break;
    	default:
    		return false;
    	}
    	return true;
    }
    The Wazaa

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The entire code (or most of it), I have no idea how you create or handle the dialog(s) there...

    You do dispatch messages twice though which is odd...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  11. #11
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    I found the error now

    This is the new code:
    Code:
    if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    		{
    			if (msg.message == WM_QUIT)
    			{
    				exit(0);
    			}
    			else
    			{
    				TranslateMessage(&msg);
    				DispatchMessage(&msg);
    			}
    			
    		}
    And the entire code is quite long, about 2.5 k lines. And spread out on too many source files

    Thanks for your replies

    The Wazaa

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing from a Rich Edit control
    By JustMax in forum Windows Programming
    Replies: 10
    Last Post: 02-14-2009, 07:12 PM
  2. Change cursor on single edit control
    By Niara in forum Windows Programming
    Replies: 3
    Last Post: 01-11-2009, 09:52 AM
  3. How can I send a text string to an edit control
    By marc74 in forum Windows Programming
    Replies: 5
    Last Post: 01-06-2005, 10:14 PM
  4. Controlling an edit control
    By master5001 in forum Windows Programming
    Replies: 2
    Last Post: 10-16-2001, 03:08 PM
  5. Rich edit control example Win API
    By Echidna in forum Windows Programming
    Replies: 1
    Last Post: 09-17-2001, 02:12 AM