How to turn off beeping when pressing ENTER on a single-line EDIT control under Windows CE?
Asked Answered
N

6

5

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.

Never answered 27/8, 2010 at 22:23 Comment(1)
I don't know if this is true for Windows CE as well, but with Desktop Windows you get keyboard navigation in a standard window by calling IsDialogMessage as part of the message dispatching. Among other things, this deals with the [Enter] key as well, invoking the default button (if there is one).Iceboat
N
16

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 );
}
Never answered 28/8, 2010 at 8:49 Comment(0)
L
2

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

Lexicostatistics answered 30/5, 2011 at 11:17 Comment(0)
R
2

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;
}
Rapier answered 12/4, 2015 at 22:37 Comment(0)
F
0

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.

Forecastle answered 27/8, 2010 at 22:41 Comment(3)
...WM_CHAR - d'oh - how much does one have to handle to stop a beep! :)Forecastle
Apparently, just that message. ^^Never
Ah - nice one, thanks for letting me know, gablin - will bear in mind for the future.Forecastle
S
0

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);
        }
    }
}
Subacid answered 4/4, 2019 at 7:25 Comment(0)
S
0

In a Windows desktop app, I got the same problem while handling VK_TAB in WM_GETDLGCODE. so I found the following solution.

SystemParametersInfo(SPI_SETBEEP, FALSE, NULL, 0); // turn of the beep
// do somthing ... //
SystemParametersInfo(SPI_SETBEEP, TRUE, NULL, 0); // turn on the beep

Stokes answered 7/12, 2019 at 15:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.