Announcement

Collapse
No announcement yet.

Label Hot Key ?

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

  • Label Hot Key ?

    Problems! Problems!
    Here is code for SUB with CALLBACK, that is supposed to work with
    hot key, but does nothing unless click on label???
    Code:
    SUB TEST1
         DIALOG NEW 0, "Label Hot Key", , , 81, 60, 0 TO hDlg52
         IF ERR OR hDlg52 = 0 THEN EXIT SUB  ' An Error occurred
         CONTROL ADD LABEL, hDlg52, %ID_MENU_HELP,   "&Help",   1,  5, 80, 14, _
          %SS_NOTIFY OR %BS_LEFT OR %BS_VCENTER OR %WS_TABSTOP, 0
         CONTROL ADD LINE, hDlg52, 1, "", 1, 16, 80, 1, %SS_BLACKFRAME, 0 
         CONTROL ADD LABEL, hDlg52, %ID_MENU_ABOUT,  "&About",  1, 18, 80, 14, _
          %SS_NOTIFY OR %BS_LEFT OR %BS_VCENTER OR %WS_TABSTOP, 0
         CONTROL SET COLOR hDlg52, %ID_MENU_HELP, %RED, -1
         DIALOG SHOW MODAL hDlg52, CALL TEST1CALLBACK
    
         ' Close hDlg52
         DIALOG END hDlg52
    END SUB
    
        CALLBACK FUNCTION TEST1CALLBACK
        LOCAL yResult AS LONG
        LOCAL sSubject  AS STRING
          SELECT CASE CBMSG
           CASE %WM_INITDIALOG          ' <- sent right before the dialog is dis
    
           CASE %WM_COMMAND             ' <- a control is calling
              SELECT CASE CBCTL         ' <- look at control's id
                 CASE %ID_MENU_HELP     ' 204
                    IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = %STN_DBLCLK THEN
                     HFlg = 1
                     DIALOG END CBHNDL, 1
                    END IF
                 CASE %ID_MENU_ABOUT    ' 205
                    CALL HABOUT
                    DIALOG END CBHNDL, 1
              END SELECT
          END SELECT
        END FUNCTION ' TEST1CALLBACK

  • #2
    >is supposed to work with hot key,

    It's not at all "supposed to" work.

    From the Documentation:
    Although static controls are child windows, they cannot be selected. Therefore, they cannot receive the keyboard focus and cannot have a keyboard interface.
    That means labels (static controls) can never respond to keystrokes.

    Use accelerators to get the effect you are after.

    (FWIW, I only had to add twenty lines of code or so to create a compilable demonstration of the "problem". Next time you post please include those 20 lines yourself).

    MCM
    Last edited by Michael Mattias; 12 Oct 2007, 07:42 AM.
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      From PBWin80, ver 8.04 help file.
      Purpose
      Add a text label to a dialog. A text label is similar to a conventional static control.
      CONTROL ADD LABEL, hDlg, id&, txt$, x, y, xx, yy [, [style&] [, [exstyle&]]] [[,] CALL callback]
      txt$
      Text to be displayed in text label. An ampersand (&) may be included in txt$ to specify a hot-key. See the Remarks section below.
      Remarks
      If the ampersand (&) character appears in the txt$ parameter, the letter that follows will be displayed underscored. This adds a control accelerator (hot-key) to enable the user to directly "click" the control that immediately follows in the Tab-Order after the Label control, simply by pressing and holding the ALT key while pressing the specified hot-key. For example, "Choose &Security Level" makes ALT+S the hot-key.
      A label control will only send messages to a callback if the %SS_NOTIFY style is used. The following notifications are sent to the Callback Function:
      %BN_CLICKED Sent when the user clicks a mouse button, or activates the button with the hot-key (unless the button has been disabled).
      %STN_DBLCLK Sent when the user double-clicks on a label control (unless the control has been disabled).
      %BN_DISABLE Sent when a button is disabled.
      %STN_ENABLE Sent when a label control has been enabled.

      Comment


      • #4
        Well, then you need to report a bug to PowerBASIC, Inc since the documented behavior does not equal the observed behavior.

        (And for them, you really need to include a COMPILABLE example demonstrating the problem). (And your compiler version).

        In the meantime, use accelerators.

        Oh, and take three minutes to update your profile with your location, will you please?

        MCM
        Michael Mattias
        Tal Systems Inc. (retired)
        Racine WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          As Michael stated, labels can't really be used to trap keystrokes. Well, they can, but it is a BIG pain. You are better off to use an accelerator table in this instance.
          Adam Drake
          Drake Software

          Comment


          • #6
            Mr. Gardner DOES have a good point, however: the documentation says it will work, and it did not work for him in version 8.04. (Nor did it work for me using 8.03).

            Either the doc is in error or the compiler is in error, but there IS an error somewhere.

            MCM
            Michael Mattias
            Tal Systems Inc. (retired)
            Racine WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              I would lean towards documentation error...
              Adam Drake
              Drake Software

              Comment


              • #8
                I just re-read the documentation, and I definitely misread it...

                If the ampersand (&) character appears in the txt$ parameter, the letter that follows will be displayed underscored. This adds a control accelerator (hot-key) to enable the user to directly "click" the control that immediately follows in the Tab-Order after the Label control, simply by pressing and holding the ALT key while pressing the specified hot-key. For example, "Choose &Security Level" makes ALT+S the hot-key.
                If there is a control on screen that can receive keyboard focus, such as a button, the label hotkey will click that control.
                Adam Drake
                Drake Software

                Comment


                • #9
                  Example

                  Code:
                  '==============================================================================
                  '
                  '  HELLODDT.BAS for PowerBASIC for Windows
                  '  Copyright (c) 1997-2005 PowerBASIC, Inc.
                  '  All Rights Reserved.
                  '
                  '  A simple DDT dialog example.
                  '
                  '==============================================================================
                  '
                  ' Original program modified to illustrate using a label like a button.
                  
                  '------------------------------------------------------------------------------
                  ' Metastatements
                  '
                  #COMPILE EXE
                  #DIM ALL
                  
                  #INCLUDE "DDT.inc"
                  
                  
                  '------------------------------------------------------------------------------
                  ' Equates and global variables
                  '
                  %IDOK       = 1
                  %IDCANCEL   = 2
                  %IDLABEL    = 100
                  %BS_DEFAULT = 1
                  
                  '------------------------------------------------------------------------------
                  ' Label callback
                  '
                  CALLBACK FUNCTION LabelOK () AS LONG
                  
                      IF CBCTLMSG = %BN_CLICKED THEN
                  		MSGBOX "Hello!  I am a Label!"
                          FUNCTION = 1
                      END IF
                  
                  END FUNCTION
                  
                  '------------------------------------------------------------------------------
                  ' Main application entry point...
                  '
                  FUNCTION PBMAIN() AS LONG
                  
                      LOCAL hDlg   AS DWORD
                      LOCAL Result AS LONG
                  
                      DIALOG FONT "Arial", 12
                  
                      '  Create a new dialog template
                      DIALOG NEW 0, "Label Button Demonstration",,, 400, 288, _
                      		%DS_CENTER OR %WS_SYSMENU TO hDlg
                  
                      '----------------------------------------------------------------
                      '  Add controls to it
                  	CONTROL ADD LABEL, hDlg, %IDLABEL, "Please click me!", _
                  			102, 70, 211, 109, %SS_CENTER OR %SS_CENTERIMAGE OR %SS_NOTIFY, _
                  			CALL LabelOK
                  
                      '----------------------------------------------------------------
                      '  Display the dialog
                      DIALOG SHOW MODAL hDlg TO Result
                  
                      '----------------------------------------------------------------
                  
                  END FUNCTION
                  Regards,
                  Bob

                  Comment


                  • #10
                    Originally posted by Robert DeBolt View Post
                    CALLBACK FUNCTION LabelOK () AS LONG

                    IF CBCTLMSG = %BN_CLICKED THEN..
                    It would be more 'proper' to use %STN_CLICKED rather than %BN_CLICKED in the callback.

                    While Win32API.inc assigns the same value to both equates (=0) you'd come unstuck if you wanted to detect a double click and used %BN_DOUBLECLICKED (=5) instead of %STN_DBLCLK (=1)
                    Rgds, Dave

                    Comment


                    • #11
                      Adam,

                      Way to go. That is the answer. I've run in to so many problems lately
                      that I'am trying to go to fast - got to slow down.

                      Comment


                      • #12
                        > "the control that immediately follows in the Tab-Order after the Label control.."

                        Boy, I missed that, too. Although I haven't tested it, that might indeed work just ducky using DDT, because I know it works exactly like that "SDK style."


                        MCM
                        Michael Mattias
                        Tal Systems Inc. (retired)
                        Racine WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment


                        • #13
                          Sample of Hot Key for Label
                          Code:
                          '==============================================================================
                          '
                          '  HELLODDT.BAS for PowerBASIC for Windows
                          '  Copyright (c) 1997-2005 PowerBASIC, Inc.
                          '  All Rights Reserved.
                          '
                          '  A simple DDT dialog example.
                          '
                          '==============================================================================
                          '
                          ' Original program modified to illustrate using a label like a button.
                          ' Modified has been Modified to illustrate using a Hot Key for Label, 10-13-2007
                          '------------------------------------------------------------------------------
                          ' Metastatements
                          '
                          #COMPILE EXE
                          #DIM ALL
                          
                          #INCLUDE "DDT.inc"
                          
                          
                          '------------------------------------------------------------------------------
                          ' Equates and global variables
                          '
                          %IDOK       = 1
                          %IDCANCEL   = 2
                          %IDLABEL    = 100
                          %IDBUT      = 101
                          %BS_DEFAULT = 1
                          
                          '------------------------------------------------------------------------------
                          ' Label callback
                          '
                          CALLBACK FUNCTION LabelOKCALLBACK () AS LONG
                              SELECT CASE CBMSG
                                 CASE %WM_COMMAND                ' <- a control is calling
                                    SELECT CASE CBCTL            ' <- look at control's id
                                       CASE %IDLABEL             ' 100
                                          IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = %STN_DBLCLK THEN
                                           DIALOG END CBHNDL, 1
                                          END IF
                                       CASE %IDBUT
                                           IF CBCTLMSG = %BN_CLICKED THEN
                                            DIALOG END CBHNDL, 1
                                           END IF
                                    END SELECT
                              END SELECT
                              'IF CBCTLMSG = %STN_CLICKED THEN
                              '
                              '    MSGBOX "I am a Hot Key now, Hello!  I am a Label!"
                              '    DIALOG END CBHNDL, 1
                              '    FUNCTION = 1
                              'END IF
                          
                          END FUNCTION
                          
                          '------------------------------------------------------------------------------
                          ' Main application entry point...
                          '
                          FUNCTION PBMAIN() AS LONG
                          
                              LOCAL hDlg   AS DWORD
                              LOCAL Result AS LONG
                          
                              DIALOG FONT "Arial", 12
                          
                              '  Create a new dialog template
                              DIALOG NEW 0, "Label Button Demonstration",,, 400, 288, _
                                      %DS_CENTER OR %WS_SYSMENU TO hDlg
                          
                              '----------------------------------------------------------------
                              '  Add controls to it
                              CONTROL ADD LABEL, hDlg, %IDLABEL, "&Please click me! or Press Hot Key", _
                                      10, 70, 150, 90, %SS_CENTER OR %SS_CENTERIMAGE OR %SS_NOTIFY ', _
                              CONTROL ADD BUTTON, hDlg, %IDBUT, "", -30, -30, -40, -50, %BS_FLAT _
                               OR %SS_NOTIFY OR %BS_LEFT OR %BS_VCENTER OR %WS_TABSTOP ', 0
                                   '   CALL LabelOK
                          
                              '----------------------------------------------------------------
                              '  Display the dialog
                              DIALOG SHOW MODAL hDlg, CALL LABELOKCALLBACK ' TO Result
                          
                              '----------------------------------------------------------------
                          
                          END FUNCTION

                          Comment

                          Working...
                          X