Capturing Mouse Events from Every Component

Capturing mouse events from every component

One straightforward way to do this is to add a message loop filter by calling Application.AddMessageFilter and writing a class that implements the IMessageFilter interface.

Via IMessageFilter.PreFilterMessage, your class gets to see any inputs messages that pass through your application's message loop. PreFilterMessage also gets to decide whether to pass these messages on to the specific control to which they're destined.

One piece of complexity that this approach introduces is having to deal with Windows messages, via the Message struct passed to your PreFilterMessage method. This means referring to the Win32 documention on WM\_LBUTTONDOWN, WM\_MOUSEMOVE, WM\_LBUTTONUP etc, instead of the conventional MouseDown, MouseMove and MouseUp events.

How can I capture all mouse events in a JFrame/Swing?

Use an AWTEventListener to filter out the MouseEvents:

long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK;
Toolkit.getDefaultToolkit().addAWTEventListener( new AWTEventListener()
{
public void eventDispatched(AWTEvent e)
{
System.out.println(e);
}
}, eventMask);

How to pass mouse events between overlapping components in ReactJS

You can pass events as props like so:

import { useState } from 'react'

const Wrapper = () => {
const [event, setEvent] = useState(null)

return (
<div onClick={(e) => setEvent(e)}>
Lorem Ipsum
<Component event={event} />
</div>
)
}

const Component = (event) => {
console.log(event) // → event: SyntheticBaseEvent
return null
}

export default Wrapper

How can I capture all mouse events in Java?

You can't do this. Not even the mouse itself reports every single pixel (or whatever unit it uses) to the computer.

You'll have to interpolate the missing points. A single linear interpolation should do the trick.

Pass all mouse events from parent to child component - Angular 6

You can create a directive to listen to all mouse events

@Directive({
selector: "[appMouseListener]"
})
export class MouseListenerDirective {
constructor() {}

private enterSub = new Subject<MouseEvent>();

public enter = this.enterSub.asObservable();

@HostListener("mouseenter", ["$event"]) onMouseEnter(event: MouseEvent) {
this.enterSub.next(event);
}
}

if you need you can filter the events emitted to only emit the parent one. This is the event target would be the parent component element.

export class MouseListenerDirective {

constructor(private el: ElementRef){}

public enter = this.enterSub.asObservable().pipe(filter(x => x.target === this.el.nativeElement));

...

add this directive on parent component

<parent-component appMouseListener>
<child-component></child-component>
</parent-component>

Access events on child component by injecting the directive

@Component({ // configs}
export class ChildComponent {

constructor(mouseListener: MouseListenerDirective){
mouseListener.enter.subscribe(x => console.log(x)); // don't forget to manage subscriptions
}
}

Capture mouse events outside of the control

Handle the WM_CAPTURECHANGED message and check whether textbox contains focus but it is not captured, if so hide it.

protected override void WndProc(ref Message m)
{
base.WndProc(ref m);

const int WM_CAPTURECHANGED = 0x0215;
if (m.Msg == WM_CAPTURECHANGED)
{
if (!textBox1.Capture && textBox1.Focused && textBox1.Visible)
{
textBox1.Visible = false;
}
}
}


Related Topics



Leave a reply



Submit