• Welcome to Valhalla Legends Archive.
 

[Win32] Unwanted beep while looking for VK_RETURN within WM_KEYDOWN

Started by warz, January 03, 2006, 06:49 PM

Previous topic - Next topic

warz

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)

MyndFyre

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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Zakath

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
Quote from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.

warz

This has been corrected by creating a WINAPI created loop, like MindFyre suggested.

Skywing

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.

warz

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;
        }
}