Handmade Hero»Forums»Code
23 posts
Auto scroll on middle mouse button click

Hey everybody!

I'd like to ask ya'll how do I implement (Using C and Windows API) auto scroll on middle mouse button click? To be clear, when we click the mouse wheel (middle button) in Firefox (for example), a round icon appears at the point of the mouse cursor, like so:

Autoscroll.png

After entering this state, pushing the mouse up or down will make the page scroll automatically in that direction.

I seem to have trouble finding any info about this feature. But Windows API is the lowest you can possibly get so there has to be a way to do this!

Any answer is appreciated <3

Mārtiņš Možeiko
2562 posts / 2 projects
Auto scroll on middle mouse button click
Edited by Mārtiņš Možeiko on

You implement that by handling mouse down/move/up messages.

on mouse down message you enable "scroll" state and remember mouse position

if (button == middle) {
  scroll_enabled = !true;
  scroll_pos = { mouse.x, mouse.y }
}

on mouse up message you disable scroll state:

if (button == middle) {
  scroll_enabled = false;
}

then in your main rendering loop you get current mouse position and do the scroll if distance is large enough

if (scroll_enabled) {
  vec2 mouse_pos = get_current_mouse_pos();
  vec2 delta = mouse_pos - scroll_pos;
  if (delta.length > min_scroll_distance) {
    do_the_scroll(delta * frameDeltaTime * 0.5f); // adjust speed as necessary
  } else {
    draw_circle_button(scroll_pos); // if not scrolling, draw the circle where scroll started
  }
}

To make it go faster or slower increase or decrease that 0.5f number.