Question

I'm developing an application targeted to a POCKET PC 2003 (Windows CE 4.2) device using C++ and native WINAPI (i.e. no MFC or the like). In it I have a single-line edit control which part of the main window (not a dialog); hence the normal behaviour of Windows when pressing ENTER is to do nothing but beep.

I've subclassed the window procedure for the edit control to override the default behaviour using the following code:


LRESULT CALLBACK Gui::ItemIdInputProc( HWND hwnd, UINT message, WPARAM wParam,
    LPARAM lParam ) {

    switch ( message ) {
        case WM_KEYDOWN :
            switch ( wParam ) {
                case VK_RETURN :
                    addNewItem();
                    return 0;
            }
    }

    return CallWindowProc( oldItemIdInputProc_, hwnd, message, wParam, lParam );
}

This causes the equivalent behaviour as pressing the 'OK' button.

Now to the problem at hand: this window procedure does not override the default behaviour of making a beep. I suspect that there must be some other message or messages which are triggered as ENTER is pressed that I fail to capture; I just can't figure out which. I really want to stop the device from beeping as it messes up other sounds that are played in certain circumstances when an item collision occurs, and it is crucial that the user is alerted about that.

Thanks in advance.

Was it helpful?

Solution

After spewing all messages to a log file, I finally managed to figure out which message was causing the beeping - WM_CHAR with wParam set to VK_RETURN. Stopping that message from being forwarded to the edit control stopped the beeping. ^^

The final code now reads:


LRESULT CALLBACK Gui::ItemIdInputProc( HWND hwnd, UINT message, WPARAM wParam,
    LPARAM lParam ) {

    switch ( message ) {
        case WM_CHAR :
            switch ( wParam ) {
                case VK_RETURN :
                    addNewItem();
                    return 0;
            }
    }

    return CallWindowProc( oldItemIdInputProc_, hwnd, message, wParam, lParam );
}

OTHER TIPS

I had the same problem but with my Rich Edit (using also subclassed callback). This side helped me lot but sadly the solution from gablin didn't work for me. Somehow I couldn't get the VK_RETURN from the WM_CHAR. But from the WM_KEYDOWN message I can:). I also found out that in my case the beep comes only if the rich edit use not the ES_MULTILINE style. So finaly this is my working solution in the Callback to dissable the beep if return key is pressed. Maybe it can still help someone who has the same problem :)

switch (message){
        case (WM_KEYDOWN) : {
                switch (wParam) {
                case VK_RETURN:
                    if ((GetWindowLong(this_editbox->getHandle(), GWL_STYLE) & ~ES_MULTILINE)){ //Only dissable return key if the rich edit is a single line rich edit                                  
                        //Do something you want to do here if return key was pressed for ex. delete text with SetWindowTextA(hRichEdit, "");     after reading
                        return 0;// stop beep by blocking message
                    }
                }
            break;
        }
        default: break;
}

Had the same issue but thanks to you, I finally managed to turn the beep off.

// Run the message loop. It will run until GetMessage() returns 0
while(GetMessage (&messages, NULL, 0, 0)) {
  if(messages.message == WM_KEYDOWN && messages.wParam == VK_RETURN) {
    sendChatMessage("sample text");
    continue;
  }

  // Translate virtual-key messages into character messages
  TranslateMessage(&messages);

  // Send message to WindowProcedure
  DispatchMessage(&messages);
}

I guess the trick was to not let execute those two statements

Try also handling the WM_KEYUP and return 0 for VK_RETURN there as well - Windows non-CE also beeps if you don't handle the key event in both down and up.

In a Windows desktop app, I was also getting annoying beeps when hitting the left arrow key when the insertion point was to the left of the first character, or hitting the right arrow key when the insertion point was positioned after the last character. This code handles the return key as well as the left and right arrow keys to stop the beep.

This is in a Windows desktop app, so I'm not hearing a beep for WM_CHAR + VK_RETURN; you'll have to try this code yourself on CE to see if it works well for you.

    bool processKeystroke = true;

    if (message == WM_CHAR || message == WM_KEYDOWN || message == WM_KEYUP) {

        DWORD start = 0;
        DWORD end = 0;
        switch (wParam) {

        case VK_RETURN:
            if ((GetWindowLong(hwnd, GWL_STYLE) & ~ES_MULTILINE)) {
                processKeystroke = false;
            }
            break;
        case VK_LEFT:
            {
                ::SendMessage(hwnd, EM_GETSEL, (WPARAM) &start, (LPARAM) &end);
                if (start == 0 && end == 0) {
                    processKeystroke = false;
                }
            }
            break;
        case VK_RIGHT:
            {
                LPARAM charCount = ::SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0);
                ::SendMessage(hwnd, EM_GETSEL, (WPARAM) &start, (LPARAM) &end);
                if (wParam == VK_RIGHT && start == charCount && end == charCount) {
                    processKeystroke = false;
                }
            }
            break;
        }

        if (processKeystroke) {
            lResult = DefSubclassProc(hwnd, message, wParam, lParam);
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top