Announcement

Collapse
No announcement yet.

Avoid Enter-key ping..

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Avoid Enter-key ping..

    Just a small tip. When we want to trap Enter key in a control, we usually have
    to sub-class the control and compare WM_KEYDOWN's wParam against VK_RETURN. To
    get it there, we also have to trap WM_GETDLGCODE and return DLGC_WANTALLKEYS.

    Okay, so far all good. We get VK_ENTER, can act on it, but in some controls we
    also get an annoying "ping" from the system. I've just struggled with this in
    a TreeView control. No matter what I did or returned in WM_KEYDOWN, system still
    pinged on Enter-key. Finally tested to use WM_GETDLGCODE directly in TreeView's
    sub-class procedure, like:
    Code:
         CASE %WM_GETDLGCODE
            IF wParam = %VK_RETURN THEN
               'expand/collapse selected item by code, if possible..
            END IF
    Voila! No need to return anything or involve WM_KEYDOWN at all. Works
    fine and no more annoying ping on Enter key.

    Haven't worked much with the treeview control before, so may be other ways
    to trap Enter-key? Used only way I know, but just couldn't get rid of that
    stupid ping, until I tested the above. Now it keeps its big mouth shut..


    ------------------

  • #2
    Stray thought that I've never tried and probably won't work, but what about defining VK_RETURN as an accelerator key (would need to process TranslateAccelerator() before IsDialogMessage() in the message pump I suppose.

    As I said, just a thought...

    ------------------
    Lance
    PowerBASIC Support
    mailto:support@powerbasic.comsupport@powerbasic.com</A>
    Lance
    mailto:lanceedmonds@xtra.co.nz

    Comment


    • #3
      Hm, yeah - should be possible with a separate id for it. Think this way is
      easiest. Long time ago, before I knew about sub-classing and WM_GETDLGCODE,
      I used a timer and GetKeyState to do it. Worked, but not pretty..


      ------------------

      Comment


      • #4
        Yes, I know this thread is quite old. But, I was looking for a way to detect "Enter" in a TreeView and Search led me here.

        Works like a charm, Borje!

        I've always loved how stuff sits around for decades, just waiting to make Gary Beene happy. Makes one feel special!

        Comment


        • #5
          A way I also like for many controls in dialogs, if the key is not rejected by the control, is to use IDOK in conjunction with GetFocus()...

          Code:
          CASE %WM_COMMAND
            SELECT CASE CBCTL
              CASE %IDOK 'Enter key
                IF CBCTLMSG = %BN_CLICKED THEN
                  IF GetFocus() = hTreeView THEN 'or IF GetFocus() = GetDlgItem(CBHNDL, %TreeView) THEN
                    WinBeep(500, 100)
                  END IF
                END IF
            END SELECT

          Comment


          • #6
            I typically convert it to another generic command on editable screens and then eat it.

            Code:
                    CASE %WM_KEYDOWN
                        SELECT CASE wParam
                            CASE %VK_RETURN
                                ' "return detected"
                                SELECT CASE gsNewScreen
            
                                    CASE "WeatherDetail"    , _     'read only screen
                                         "CommandRecord"            'read only screen
                                        'eat this command
                                        CALL DefaultBeep
                                        FUNCTION = 0
                                        EXIT FUNCTION
            
                                    CASE "HelpFind"         , _
                                         "CreateMessage"    , _
                                         "NewPlaylist"      , _
                                         "SearchPlaylist"   , _
                                         "UserName"         , _
                                         "CreateScript"     , _
                                         "HOUR"          
                                        SendMessageA hWndMain, %WM_COMMAND, %ID_Button1Enter, 0
            
                                END SELECT
                                FUNCTION = 0
                                EXIT FUNCTION    'this must be here to stop default behavior
            NOTE:
            %ID_Button1Enter is a converge command for hard keyboard, soft keyboard, mouse click on standalone enter button, and general voice ENTER command for all screens.
            Last edited by Jim Fritts; 7 May 2019, 12:32 PM.

            Comment

            Working...
            X