Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How do I get recurring events from holding a button down?

How do I get recurring events from holding a button down?

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    doug25
    wrote on last edited by
    #1

    Hi, I have a button that is supposed to increase the size of an image when it's held down. I want it so that when the button is held down, I get a repeated event. How do I know in my application that a button is held down, is there an event generated that is sent to the window procedure ?

    enhzflepE 1 Reply Last reply
    0
    • D doug25

      Hi, I have a button that is supposed to increase the size of an image when it's held down. I want it so that when the button is held down, I get a repeated event. How do I know in my application that a button is held down, is there an event generated that is sent to the window procedure ?

      enhzflepE Offline
      enhzflepE Offline
      enhzflep
      wrote on last edited by
      #2

      You'll get a WM_CHAR message sent each time (a) a key is pressed or (b) a key has been held for long enough to trigger another 'key-pressed' event. Here's a minimal WindowProcedure

      LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
      {
      switch (message) /* handle the messages */
      {
      case WM_DESTROY:
      PostQuitMessage (0); /* send a WM_QUIT to the message queue */
      break;
      case WM_CHAR:
      printf("Key Pressed: %c\n", wParam);
      break;
      default: /* for messages that we don't deal with */
      return DefWindowProc (hwnd, message, wParam, lParam);
      }
      return 0;
      }

      D 1 Reply Last reply
      0
      • enhzflepE enhzflep

        You'll get a WM_CHAR message sent each time (a) a key is pressed or (b) a key has been held for long enough to trigger another 'key-pressed' event. Here's a minimal WindowProcedure

        LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
        {
        switch (message) /* handle the messages */
        {
        case WM_DESTROY:
        PostQuitMessage (0); /* send a WM_QUIT to the message queue */
        break;
        case WM_CHAR:
        printf("Key Pressed: %c\n", wParam);
        break;
        default: /* for messages that we don't deal with */
        return DefWindowProc (hwnd, message, wParam, lParam);
        }
        return 0;
        }

        D Offline
        D Offline
        doug25
        wrote on last edited by
        #3

        thanks for the reply, but when i say button I mean a button control not a keyboard key. I'm looking for an event such as WM_COMMAND that is generated while the button is pressed until the button is released. WM_COMMAND though just is generated on click not press as well.

        enhzflepE C 2 Replies Last reply
        0
        • D doug25

          thanks for the reply, but when i say button I mean a button control not a keyboard key. I'm looking for an event such as WM_COMMAND that is generated while the button is pressed until the button is released. WM_COMMAND though just is generated on click not press as well.

          enhzflepE Offline
          enhzflepE Offline
          enhzflep
          wrote on last edited by
          #4

          Since the keyboard auto-repeat, some of the 'low'-level stuff is already done. If however, you want a button control to have auto-repeat, you'll have to set a timer yourself. I'd imagine that you would set(create) the timer when the particular button was pressed. Each time the timer is triggered, you'd check to see if the button's state indicated that it was pressed. If so, simply send yourself another WM_COMMAND message. HOWEVER, this task is somewhat complicated by the fact that the WM_COMMAND is not sent by a button until either the mouse-button or the keyboard key that was used to press it is released, meaning that your first event is not fired until after the button is no longer pressed, hence no ability to auto-repeat. I suspect that you'll need to create this button as a custom-control, handling both the drawing (reasonably easy using the DrawThemeBackground(sp?) function) and the keyboard/mouse handling. Mouse handling should be pretty straight forward, using SetCapture and ReleaseCapture. You'll have to also work out which keyboard keys you want to be able to press the button too, setting the button's state to BS_PRESSED. This sounds like about the kind of functionality that the spin-button control offers, albeit with a single button rather than a pair of them. To that end, to find a solution quickly, I'd probably start looking for code for a custom spin-button control, editing as needed to fulfil your needs.

          D 1 Reply Last reply
          0
          • D doug25

            thanks for the reply, but when i say button I mean a button control not a keyboard key. I'm looking for an event such as WM_COMMAND that is generated while the button is pressed until the button is released. WM_COMMAND though just is generated on click not press as well.

            C Offline
            C Offline
            Code o mat
            wrote on last edited by
            #5

            See if you get BN_PUSHED[^] and BN_UNPUSHED[^] from the button, these are, as the documentation states, provided only for compatibility with 16-bit versions of Windows, so they might not work anymore, but if they do, you could try starting the timer with SetTimer[^] on BN_PUSHED and kill it with KillTimer[^] on BN_UNPUSHED, and do your zooming thing in the handler for WM_TIMER[^].

            > The problem with computers is that they do what you tell them to do and not what you want them to do. < > If it doesn't matter, it's antimatter.<

            1 Reply Last reply
            0
            • enhzflepE enhzflep

              Since the keyboard auto-repeat, some of the 'low'-level stuff is already done. If however, you want a button control to have auto-repeat, you'll have to set a timer yourself. I'd imagine that you would set(create) the timer when the particular button was pressed. Each time the timer is triggered, you'd check to see if the button's state indicated that it was pressed. If so, simply send yourself another WM_COMMAND message. HOWEVER, this task is somewhat complicated by the fact that the WM_COMMAND is not sent by a button until either the mouse-button or the keyboard key that was used to press it is released, meaning that your first event is not fired until after the button is no longer pressed, hence no ability to auto-repeat. I suspect that you'll need to create this button as a custom-control, handling both the drawing (reasonably easy using the DrawThemeBackground(sp?) function) and the keyboard/mouse handling. Mouse handling should be pretty straight forward, using SetCapture and ReleaseCapture. You'll have to also work out which keyboard keys you want to be able to press the button too, setting the button's state to BS_PRESSED. This sounds like about the kind of functionality that the spin-button control offers, albeit with a single button rather than a pair of them. To that end, to find a solution quickly, I'd probably start looking for code for a custom spin-button control, editing as needed to fulfil your needs.

              D Offline
              D Offline
              doug25
              wrote on last edited by
              #6

              thanks, I've managed to get the holding button down to work. I just used a global veriable to track the state of the button via the WM_DRAWITEM notification and then in my program loop referred to this variable.

              enhzflepE 1 Reply Last reply
              0
              • D doug25

                thanks, I've managed to get the holding button down to work. I just used a global veriable to track the state of the button via the WM_DRAWITEM notification and then in my program loop referred to this variable.

                enhzflepE Offline
                enhzflepE Offline
                enhzflep
                wrote on last edited by
                #7

                Bewdy! :thumbsup: Happy to help you scramble over that little obstacle.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups