Valhalla Legends Forums Archive | C/C++ Programming | [Win32] Unwanted beep while looking for VK_RETURN within WM_KEYDOWN

AuthorMessageTime
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:

[code]
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;
}
}
}
}
}
[/code]

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:

[code]
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");
}
}
[/code]

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 ([url]http://www.torque.ircds.darkstarllc.com/Files/soupbot2.zip[/url])
January 4, 2006, 12:49 AM
Myndfyr
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.
[/quote]

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.
January 4, 2006, 12:58 AM
Zakath
Don't catch WM_KEYDOWN. Catch WM_CHAR instead. That takes care of the beep problem.

[code]case WM_CHAR:
if ( (TCHAR)wParam == '\r' ) { //Catches press of Enter[/code]
January 5, 2006, 8:19 PM
warz
This has been corrected by creating a WINAPI created loop, like MindFyre suggested.
January 5, 2006, 8:50 PM
Skywing
[quote author=warz link=topic=13766.msg140656#msg140656 date=1136494243]
This has been corrected by creating a WINAPI created loop, like MindFyre suggested.
[/quote]

In most programs you would accomplish this via subclassing the edit control, BTW.
January 5, 2006, 9:24 PM
warz
It's subclassed, I meant.

[code]
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);
}
[/code]

[code]
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;
        }
}
[/code]
January 5, 2006, 10:29 PM

Search