How to Capture the Mouse Move Event

How do I capture the mouse move event

You could use a low level mouse hook. See this example and check for the WM_MOUSEMOVE mesage in HookCallback.

You could also use the IMessageFilter class to catch the Mouse Events and trigger an event to get the position (note: this will only get the position over the window, not outside of it):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GlobalMouseEvents
{
public partial class Form1 : Form
{
public Form1()
{
GlobalMouseHandler gmh = new GlobalMouseHandler();
gmh.TheMouseMoved += new MouseMovedEvent(gmh_TheMouseMoved);
Application.AddMessageFilter(gmh);

InitializeComponent();
}

void gmh_TheMouseMoved()
{
Point cur_pos = System.Windows.Forms.Cursor.Position;
System.Console.WriteLine(cur_pos);
}
}

public delegate void MouseMovedEvent();

public class GlobalMouseHandler : IMessageFilter
{
private const int WM_MOUSEMOVE = 0x0200;

public event MouseMovedEvent TheMouseMoved;

#region IMessageFilter Members

public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_MOUSEMOVE)
{
if (TheMouseMoved != null)
{
TheMouseMoved();
}
}
// Always allow message to continue to the next filter control
return false;
}

#endregion
}
}

How can I track mouse move events in javascript once the cursor is off of the target element?

no, it is not possible to get a mousemove event when the movement is outside of the attached element. As you stated correctly, you can track the movement on the document. When using that together with mouseenter and mouseleave events, you can set/unset a variable to indicate if the movements are happening on the element or not.

e.g.

var myElement = document.getElementById("special");
var isOverMyElement = false;
document.addEventListener('mousemove', function(e){
if (isOverMyElement){
console.log("mouse is within myelement at details", e);
}
})
myElement.addEventListener('mousenter', function(e){
isOverMyElement = true;
})
myElement.addEventListener('mousenter', function(e){
isOverMyElement = false;
})

If you are targeting older browsers, use jQuery as it normalizes mouseenter/leave over browsers. If you are targeting modern browsers, there is no need for jQuery

Which event do I need to capture to catching a mouse move while mousedown with Vue

I would suggest that you use mousedown, mousemove, and mouseup and possibly their touch semi-equivalents (if using touch devices).

Here is an example, with comments in the code.

const colors = ["powderblue","gold","orangered","deeppink", "deepskyblue","palegreen"];

const generateTiles = (wx, wy) => {
const ret = [];
for (let y = 0; y < wy; y++) {
const row = [];
ret.push(row)
for (let x = 0; x < wx; x++) {
row.push({x,y,background: colors[0]})
}
}
return ret;
}

Vue.createApp({
el: '#app',
setup() {
const tiles = Vue.reactive(generateTiles(10,10)); // setup tiles and make reactive
const color = Vue.ref(1); // helper for managing color(s)
let mouseDown = false; // no need to make reactive

// do some stuff to the tile here
const setTileBg = (tile) => {
tile.background=colors[color.value];
}

const handlers = {
// reuse handler, used both for parent and
onMouseDown: (tile) => {
mouseDown = true;
if(tile) setTileBg(tile);
},
onMouseUp: (e) => {
mouseDown = false;
},
onMouseOver: (tile) => {
if(mouseDown) setTileBg(tile);
}
}

// adding it to window (instead of an element) allows
// capturing mouseup event even when mouse is not within
// the dom element
window.addEventListener('mouseup', handlers.onMouseUp)


return {
tiles, color, colors, ...handlers
}
}
}).mount('#app')
.tiles,select{width:200px;margin:0 auto;display:block}.tile{display:inline-block;width:20px;height:20px;border:1px solid #000;box-sizing:border-box;font-size:10px;font-family:monospace;cursor:pointer;color:rgba(0,0,0,.4);text-align:center;padding:3px}.row{display:block;height:20px;width:200px;box-sizing:content-box;user-select:none}
<script src="https://unpkg.com/vue@3.0.2/dist/vue.global.prod.js"></script>
<div id="app">
<div>
<select v-model="color">
<option :value="i" v-for="(c,i) in colors" :style="{'background-color':c}">{{c}}</option>
</select>
</div>

<div class="tiles">
<div v-for="(row, y) in tiles" class="row"
:key="'row_' + y"
>
<div v-for="(tile, x) in row" class="tile"
:style="{'background-color':tile.background}"
:key="'tile_' + x + '_' + y"
@mouseDown="onMouseDown(tile)"
@mouseOver="onMouseOver(tile)">
{{ y * row.length + x}}
</div>
</div>
</div>
</div>

How to get the MouseMove event when the mouse actually moves over an element

As i learned more about Dragging i will answer this question myself. The Drag is an operation that should start when the mouse is moved and the left mouse button is down, but it should not start right away, to prevent accidentally started Drags the SystemParameters.MinimumHorizontalDragDistance and SystemParameters.MinimumVerticalDragDistance static members should be used.

private Point startPoint;

private void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
startPoint = e.GetPosition(null);
}

private void OnPreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Point position = e.GetPosition(this);

if (Math.Abs(position.X - startPoint.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(position.Y - startPoint.Y) > SystemParameters.MinimumVerticalDragDistance)
{
StartDrag(e);
}
}
}

This solved the issue asked in this question since the drag will never start if the mouse did not changed its position.

How to capture MouseMove event in a MFC Dialog Based application for a checkbox?

Thanks for your replies.. I found a way to get the mousemove event for my app.

WM_SETCURSOR windows message gets the mouse move. It returns the Cwnd pointer for a control and the dialog.

Find my code below.

BOOL CMyDialog::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
CWnd* pWndtooltip = GetDlgItem(IDC_STATIC_TOOLTIP);

if (pWnd != this)
{
if (IDC_SN_START_ON == pWnd->GetDlgCtrlID())
pWndtooltip->ShowWindow(SW_SHOW);

}
else
pWndtooltip->ShowWindow(SW_HIDE);

SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));

return true;

}

Receiving MouseMove events from parent control

So it seems that while the button is depressed and the mouse moved back over the underlying panel, the panel's MouseMove event is not fired.

You can capture the pointer position at this time by hooking into the button's MouseMove. BUT, the pointer's position will be relative to the button, not the panel, so you need to add these coordinates to the button's location coordinate:

Point mousePoint;

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
mousePoint = e.Location;
}

private void button1_MouseMove(object sender, MouseEventArgs e)
{
mousePoint = new Point(button1.Location.X + e.Location.X, button1.Location.Y + e.Location.Y);
}

Detect mousemove but let click events pass through

In that case you need to get the mousemove events from the window, not the overlay.

$(window).mousemove( function(e){
$('.v').css('left', e.clientX + 'px');
$('.h').css('top', e.clientY + 'px');
});

Demo: http://jsfiddle.net/jze4acsd/1/

(Or if window is too broad for you, use the nearest parent of the elements you are interacting with - it would give you the same coordinates as the overlay)

Angular EventListener for mousemove event only fires on CLICK, but not on MOVE

The stackblitz that you linked shows this:

@HostListener('document:click', ['$event']) documentClickEvent($event: MouseEvent) {
console.log('Through HostListener - Click Event Details: ', $event)
}

The HostListener is listening to click events, so your logging will only be triggered by clicking on the screen.

I copied the code snippet you have in your post (so that HostListener is listening to document:mousemove), and that triggered the logging on moving the mouse as expected.



Related Topics



Leave a reply



Submit