Announcement

Collapse
No announcement yet.

Repeating buttons

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

  • Repeating buttons

    How do you make a button act like the computer keyboard- repeating the action after a short wait?

    Conrad

  • #2
    Code:
    #COMPILE EXE
    #DIM ALL
    
    
    #INCLUDE "WIN32API.INC"
    
    FUNCTION PBMAIN () AS LONG
        LOCAL hDlg AS LONG
    
        DIALOG NEW  %NULL, "Repeat Button", 10,10, 140,100 TO hDlg
        CONTROL ADD BUTTON, hDlg, 101, "Press <SPCBAR> and hold", 10,10,120, 14
        DIALOG SHOW MODAL Hdlg CALL CBPROC
    END FUNCTION
    
    CALLBACK FUNCTION cbProc() AS LONG
    
      LOCAL hCtrl AS LONG, dwProc AS DWORD
    
       SELECT CASE CBMSG
           
           CASE %WM_INITDIALOG
               CONTROL HANDLE CBHNDL, 101 TO hCtrl
               dwProc = SetWindowLong (hctrl, %GWL_WNDPROC, CODEPTR(BtnProc))
               SetWindowLong hctrl, %GWL_USERDATA, dwProc
               
               
           CASE %WM_COMMAND
               IF CBCTL = 101 THEN
                   MessageBeep  %MB_OK
               END IF
               
           CASE %WM_DESTROY
               CONTROL HANDLE CBHNDL, 101 TO hCtrl
               SetWindowLong hCtrl, %GWL_WNDPROC, GetWindowLong (hCtrl, %GWL_USERDATA)
               
               
               
       END SELECT
    
    END FUNCTION
    
    FUNCTION BtnPRoc (BYVAL hWnd AS LONG, BYVAL wMSG AS LONG, BYVAL wParam AS LONG, BYVAL lPAram AS LONG) AS LONG
    
       SELECT CASE AS LONG wMSg
           CASE %WM_KEYDOWN
               SendMessage hWnd, %BM_CLICK, %NULL, %NULL
               
       END SELECT
       
       FUNCTION = CallWIndowProc (GetWindowLong(hWnd, %GWL_USERDATA), hWNd, wMSg, wParam, lparam)
    END FUNCTION
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      I tried MCM's version (recompiled under 9) and found nothing, then I tried my "Virtual Keyboard"http://www.powerbasic.com/support/pb...rtual+keyboard that I posted a while back

      Lo and behold, I found MCM's version to not work, and then to find mine not work

      Someone elses work...I could chalk up to "Ok was good at its time"....My Own....I could SHOULD chalk up to "It was good at its time"...but BOTH???....now I gotta wonder what changed? or what is in common that could affect 2 different scenario's???

      I am not sure WHAT changed, but PB9 compile will compile fine, but not run as advertised when I 1st created it....Pb8 will though

      What changed??? who knows unless we can all find a commonality


      Time to revist examples of the past, and how to change for future I bet
      Engineer's Motto: If it aint broke take it apart and fix it

      "If at 1st you don't succeed... call it version 1.0"

      "Half of Programming is coding"....."The other 90% is DEBUGGING"

      "Document my code????" .... "WHYYY??? do you think they call it CODE? "

      Comment


      • #4
        Works here PB/Win 9, Win XP/Pro SP3 all updates, IDE compile/run.

        Click on the button to gain focus, then press and hold down the spacebar and you hear the little "clinking" from the messagebeep repeating just like (well, because it IS) the typematic repeat from the keyboard.

        You didn't disable the typematic key repeat on your system, did you? (I don't even know if you can do that).


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

        Comment


        • #5
          M, it doesn't work here either (PB9, XP Sp3, JFPro). No sounds at all. Control gets hit okay (I inserted a MsgBox to check) just no beep, groan or even whisper. MessageBeep returns 1 which indicates it was processed without Error (successfully) so maybe it's my machine.

          ============================
          "I love criticism
          as long as it's unqualified praise."
          Noel Coward
          ============================
          It's a pretty day. I hope you enjoy it.

          Gösta

          JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
          LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

          Comment


          • #6
            Thanks MCM, compiles fine on PB9 here, but I'm not sure my description was adaquate. The spacebar repeats, and it's cool the way they're linked, but the button doesn't repeat. What I want to do is have a button in my app that, if clicked, does the expected function, but if held down, repeats the function- this is to run a positioning motor. That is, I want it like the typematic (is that what they call it?) feature of the keyboard, but i don't need the keyboard at all.

            BTW, maybe off topic, but I had some VB code at work that was fine when we wrote it a few months ago, but won't run at all on any machine I've tried it on recently. I'm suspicious that some Microsoft update changed something it shouldn't have.

            Comment


            • #7
              What I want to do is have a button in my app that, if clicked, does the expected function, but if held down, repeats the function
              Huh?

              That's exactly what the posted code does!!!

              For DEMONSTRATION purposes, I had the code "make a noise, " the motor positioning code not having been made available to me. You can make it do anything you want.

              Or did you mean, you want to have it repeat something when the left mouse button is held down?

              For that, I think you may need to capture the mouse, or maybe use the TrackMouseEvent() series of functions.
              Michael Mattias
              Tal Systems Inc. (retired)
              Racine WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                ]BTW, maybe off topic, but I had some VB code at work that was fine when we wrote it a few months ago, but won't run at all on any machine I've tried it on recently. I'm suspicious that some Microsoft update changed something it shouldn't have.
                Maybe not off topic, as someone else has a current thread describing a 'software rot' problem (software rot: program which has worked for years suddenly stops working for no apparent reason).

                Anyway... in my experience the most common cause of this is programmer error; however, the operating system either ignores or forgives this error in one version, then "tightens up the enforcement" in an update.

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

                Comment


                • #9
                  BTW, if I have made a good guess about "hold down the mouse to make something repeat" I'm not so sure a button control is the best choice.

                  Maybe an updown control... I know that handles repeating with 'time spent depressed"
                  Michael Mattias
                  Tal Systems Inc. (retired)
                  Racine WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    Language will be the death of me. In my mind, "button" always refers to either the screen button or the mouse button, whichever I'm thinking about. Yes, what I actually envisioned was holding down the left mouse button, just like holding a momentary mechanical switch on a motor- any time the switch is closed, the motor runs. You can just bump it slightly, or run it continuously. I'll look at the updown control, as that might be easier.

                    Thanks,
                    Conrad

                    Comment


                    • #11
                      If instead of "press and hold" you can live with "click on, click off" , BS_PUSHLIKE buttons might be of use:

                      http://www.powerbasic.com/support/pb...4&postcount=11
                      Michael Mattias
                      Tal Systems Inc. (retired)
                      Racine WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment


                      • #12
                        I've done this, for a virtual keyboard, via SetWindowsHookEx and mouse_event and it's not easy or pretty!

                        If you're just concerned with one or two buttons, instead of acting on the button press directly, use button down to start a timer, button up to stop it, and perform your actions on the timer events.
                        - LJ

                        Comment


                        • #13
                          Click-o-matic button

                          Along the lines that Laurence suggests..
                          Code:
                          #Compile Exe
                          #Dim All
                          #Include "WIN32API.INC"
                           
                          %BTN_Test      = 102
                          %LBL_LABEL1    = 101
                           
                          Function GetKBDelay(KBDelay As Dword) As Dword          ' TT Tonny Bjorn
                            ' If the SystemParametersInfo() function succeeds, the return value is nonzero.
                           Function = SystemParametersInfo(%SPI_GETKEYBOARDDELAY, 0, KBDelay, 0)
                          End Function 
                          '------------------/GetKBDelay
                           
                          Function GetKBSpeed(KBSpeed As Dword) As Dword
                            ' If the SystemParametersInfo() function succeeds, the return value is nonzero.
                           Function = SystemParametersInfo(%SPI_GETKEYBOARDSPEED, 0, KBSpeed, 0)
                          End Function 
                          '------------------/GetKBSpeed
                           
                          Function SubClassProc(ByVal hWnd As Dword, ByVal wMsg As Dword, _
                                                ByVal wParam As Dword, ByVal lParam As Long) As Long
                           Local lRes, oldProc, KBDelay, KBSpeed As Dword 
                           
                            Dialog Get User GetParent(hWnd), 1 To oldProc
                            GetKBDelay(KBDelay) : KBDelay = (KBDelay + 1) * 250   ' SPI_GETKEYBOARDDELAY (0 - 3 = 250 - 1000ms)
                            GetKBSpeed(KBSpeed) : KBSpeed = 400 - (KBSpeed * 12)  ' SPI_GETKEYBOARDSPEED (0 - 31 = 400 - 30ms)
                           
                            Select Case As Long wMsg
                              Case %WM_LBUTTONDOWN
                                SetTimer hWnd, 1, KBDelay, 0                      ' Set timer for repeat delay
                           
                              Case %WM_LBUTTONUP
                                KillTimer hWnd, 1 : KillTimer hWnd, 2
                                WinBeep 800, 20
                           
                              Case %WM_TIMER
                                Select Case wParam
                                  Case 1
                                    SetTimer hWnd, 2, KBSpeed, 0                  ' Set timer for repeat speed
                                  Case 2                              
                                    KillTimer hWnd, 1
                                    WinBeep 800, 20
                                End Select
                           
                            End Select
                           
                           Function = CallWindowProc(oldProc, hWnd, wMsg, wParam, lParam)
                          End Function
                          '------------------/SubClassProc
                           
                          CallBack Function DlgProc()
                           Local hBtn, oldProc As Dword
                            Select Case As Long CbMsg
                              Case %WM_INITDIALOG                                 ' Subclass the button
                                Control Handle CbHndl, %BTN_Test To hBtn
                                oldProc = SetWindowLong(hBtn, %GWL_WNDPROC, CodePtr(SubClassProc))
                                Dialog Set User CbHndl, 1, oldProc
                           
                              Case %WM_COMMAND
                                Select Case As Long CbCtl
                                  Case %BTN_Test
                                    If CbCtlMsg = %BN_CLICKED Then
                                      'MsgBox "Test"
                                    End If
                                End Select
                           
                            End Select
                          End Function
                          '------------------/DlgProc
                           
                          Function PBMain()
                           Local hDlg  As Dword
                           
                            Dialog New 0, "Click-o-matic Button test", , , 200, 120, %WS_CAPTION Or %WS_SYSMENU, To hDlg
                            Control Add Label,  hDlg, %LBL_LABEL1, "Click'n'hold", 75, 25, 120, 20
                            Control Add Button,  hDlg, %BTN_Test, "Test", 75, 60, 50, 15
                           
                            Dialog Show Modal hDlg, Call DlgProc
                           
                          End Function
                          '------------------/PbMain
                          Rgds, Dave

                          Comment


                          • #14
                            this is to run a positioning motor.
                            That ALMOST slipped by me.....What is the motor? and what is the controller? I assume what you are looking at is a "Virtual Jog" depending on the key that is pressed????
                            Engineer's Motto: If it aint broke take it apart and fix it

                            "If at 1st you don't succeed... call it version 1.0"

                            "Half of Programming is coding"....."The other 90% is DEBUGGING"

                            "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                            Comment


                            • #15
                              How about three buttons....
                              Code:
                                <Jog>
                                <Run>  
                                <Stop>
                              ???
                              Michael Mattias
                              Tal Systems Inc. (retired)
                              Racine WI USA
                              [email protected]
                              http://www.talsystems.com

                              Comment


                              • #16
                                How about three buttons....
                                Code:
                                <Jog>
                                <Run>
                                <Stop>
                                ???
                                Easy answers
                                Jog = Push a button....keep going till I let go (only problem is not known at run time
                                Run = Run something known at run time
                                Stop = Stop something known at run time


                                I can see why the original problem and the reason why to describe the problem as simple as possible WITHOUT confusing with other concepts that may be inherently similar but with absolutely nothing in common with what is trying to be achieved.
                                (well one thing, but thats why confusion comes into play)
                                Engineer's Motto: If it aint broke take it apart and fix it

                                "If at 1st you don't succeed... call it version 1.0"

                                "Half of Programming is coding"....."The other 90% is DEBUGGING"

                                "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                                Comment


                                • #17
                                  >> How do you make a button act like the computer keyboard- repeating the action after a short wait?

                                  No need for confusion, that's a pretty simple question.

                                  >> How about three buttons....
                                  How about a single button...
                                  Code:
                                    <Jog>     Click Once
                                    <Run>     Click and Hold
                                    <Stop>    Release
                                  See my post above (#13). Try it. Compiles in PBWin8 or 9.
                                  Rgds, Dave

                                  Comment


                                  • #18
                                    Dave, well done! That's exactly what I need, though no doubt I'll have to adjust the timing a bit to match the motor response. Regarding three buttons, I have software set up that way now, and it's a bit of a clunky interface, thus my desire to accomplish the same thing in a more intuitive way using a single button. Thanks!

                                    Comment


                                    • #19
                                      Yeah, no need to stick with the keyboard repeat timings. You can hard-wire the timer periods to suit your hardware and get rid of that fussy SystemParametersInfo() stuff.

                                      I thought you could also add %WS_TABSTOP to the button and capture the space bar in the subclass proc. Then you could also use the keyboard to control the button (motor) by trapping the WM_KeyDown, KeyUp messages.
                                      That's something I do with one of my field servicing programs. It's quite handy to tab onto the button then toggle it just by reaching over and slapping the space bar while your main attention is elsewhere.
                                      Rgds, Dave

                                      Comment


                                      • #20
                                        Dave, you hit the nail on the head. When you're adjusting something, your attention is on the something, not on which tiny button you're trying to hit with the mouse. Having one button is better, and the keyboard option is also good because you can do it by feel.

                                        Comment

                                        Working...
                                        X