Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: warz on January 03, 2006, 06:49 PM

Title: [Win32] Unwanted beep while looking for VK_RETURN within WM_KEYDOWN
Post by: warz on January 03, 2006, 06:49 PM
My problem is that when I hit enter in my edit control for sending chat to bnet, it makes a beeping sound. Here's my Msg loop:


void MsgLoop()
{
for(;;){
MSG Msg;
while(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
switch(Msg.message) {
case WM_KEYDOWN:
{
if(Msg.wParam == VK_RETURN)
DoSendText();
break;
}
}
}
}
}


Simply looks for the WM_KEYDOWN event, then checks if it was the return key. If it was, then we proceed to the DoSendText function:


void DoSendText(void) {
if(szLocalAccountInfo.Connected) {
char SendText[200];
GetWindowText(GetDlgItem(hMainDlg, IDC_BNSEND), SendText, sizeof(SendText));
if(strlen(SendText) > 0) {
SetWindowText(GetDlgItem(hMainDlg, IDC_BNSEND), "");
if(SendText[0] == '/') {
HandleCommand((char *)SendText, false);
} else {
AppendTextTS(hBNChat, WHITE, "<");
AppendText(hBNChat, TEAL, "%s", szLocalAccountInfo.szRealUsername);
AppendText(hBNChat, WHITE, "> %s\n", SendText);
Send(sckBNCS, "%s", SendText);
}
}
} else {
AppendText(hBNChat, RED, "Not connected\n");
}
}


Somewhere during this process I'm forgetting something, resulting in the beep. Anyone know why?

edit: feel free to download the bot and witness the beep for yourself (http://www.torque.ircds.darkstarllc.com/Files/soupbot2.zip)
Title: Re: [Win32] Unwanted beep while looking for VK_RETURN within WM_KEYDOWN
Post by: MyndFyre on January 03, 2006, 06:58 PM
Return 0, don't just break.  That's how you indicate to Windows that you've handled the message.

Quote
Return Value

An application should return zero if it processes this message.

Since you're not doing it in a WINAPI exported loop, though, you might be better off checking to see if it's WM_KEYDOWN *before* calling DispatchMessage.  IIRC, DispatchMessage tells the DefWindowProc to perform the normal window tasks, which in this case, include beeping.
Title: Re: [Win32] Unwanted beep while looking for VK_RETURN within WM_KEYDOWN
Post by: Zakath on January 05, 2006, 02:19 PM
Don't catch WM_KEYDOWN. Catch WM_CHAR instead. That takes care of the beep problem.

case WM_CHAR:
if ( (TCHAR)wParam == '\r' ) { //Catches press of Enter
Title: Re: [Win32] Unwanted beep while looking for VK_RETURN within WM_KEYDOWN
Post by: warz on January 05, 2006, 02:50 PM
This has been corrected by creating a WINAPI created loop, like MindFyre suggested.
Title: Re: [Win32] Unwanted beep while looking for VK_RETURN within WM_KEYDOWN
Post by: Skywing on January 05, 2006, 03:24 PM
Quote from: warz on January 05, 2006, 02:50 PM
This has been corrected by creating a WINAPI created loop, like MindFyre suggested.

In most programs you would accomplish this via subclassing the edit control, BTW.
Title: Re: [Win32] Unwanted beep while looking for VK_RETURN within WM_KEYDOWN
Post by: warz on January 05, 2006, 04:29 PM
It's subclassed, I meant.


WNDPROC wpOrigEditBoxProc;

LRESULT CALLBACK EditBoxSubclass(HWND EditBox, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg){
case WM_KEYUP:
if(wParam == VK_RETURN){
DoSendText();
return 0;
}
case WM_KEYDOWN:
if(wParam == VK_RETURN)
return 0;
case WM_CHAR:
if(wParam == VK_RETURN)
return 0;
}
return CallWindowProc(wpOrigEditBoxProc, EditBox, uMsg, wParam, lParam);
}



BOOL CALLBACK MainDialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg){
case WM_INITDIALOG:
        ...
wpOrigEditBoxProc = (WNDPROC)SetWindowLong(GetDlgItem(hDlg, IDC_BNSEND),
                  GWL_WNDPROC, (LONG)EditBoxSubclass);
         ...
        break;
        }
}