My question is, what's the different between event preview and event handler in GWT.
There is a callback function boolean onEventPreview(Event event) for event preview and a callback function void onBrowserEvent(Event event) as well. They are pretty similar, so what's the different between them? Especially when should I use the event preview at all when the event handler works perfect?
thanks
DOM.addEventPreview(EventPreview preview) lets you place an event preview on top of the event stack, which is called before any onBrowserEvent(Event event) is fired. This way you can place some logic before the event firing takes place. You can even prevent the event from firing by returning false. For example below example prevents the browser from reacting to mousemove and mousedown events.(Click and drag an image, browser won't drag an outline of image)
DOM.addEventPreview(new EventPreview() {
#Override
public boolean onEventPreview(Event event) {
switch (DOM.eventGetType(event)){
case Event.ONMOUSEDOWN:
case Event.ONMOUSEMOVE:
event.preventDefault();
}
return true;
}
});
Just a reminder, adding eventPreviews this way is depreciated. Correct way to do it is to use Event.addNativePreviewHandler(NativePreviewHandler handler)
From the javadoc:
As long as this preview remains on the top of the stack, it will receive all events before they are fired to their listeners. Note that the event preview will receive all events, including those received due to bubbling, whereas normal event handlers only receive explicitly sunk events.
You can return false from onEventPreview to cancel the event, in which case the event handlers will not be fired.
Related
For example: a uwp pivot automatically handles Ctrl+Tab. I want to create a custom app-wide keyboard shortcut that uses Ctrl+Tab. However, if my app is currently focused on a pivot control, then the pivot steals the shortcut keydown event.
What I have tried:
For testing purposes, I created a singleton class that handles CoreWindow.KeyDown events. I register some handlers. When I press Ctrl+Tab without focusing on any element, then the singleton class handles the event. But if I focus on a pivot control in my app and press Ctrl+Tab, then the pivot steals the event and the singleton class does not. How do I make it so the singleton class picks up the event and not the focused element?
I tried to register the KeyDown event on CoreWindow. After testing, Pivot receives keyboard events earlier than CoreWindow.KeyDown events, so you can try to disable the corresponding keyboard accelerator in Pivot:
private void Pivot_ProcessKeyboardAccelerators(UIElement sender, ProcessKeyboardAcceleratorEventArgs args)
{
if(args.Key==Windows.System.VirtualKey.Tab && args.Modifiers == Windows.System.VirtualKeyModifiers.Control)
{
args.Handled = true;
}
}
By listening to the Pivot.ProcessKeyboardAccelerators event, we can prevent the default behavior when Ctrl+Tab is triggered (args.Handled = True).
However, it should be noted that because the event is prevented from further bubbling, the CoreWindow.KeyDown event will still not be triggered at this time.
You mentioned in the question description that you used a singleton to handle the event triggered by the shortcut. Then you need to directly call your processing method in the singleton in the code.
...
args.Handled = true;
CtrlAndTabHandle();
Thanks.
Subclassing NSButton to capture mouseUp and mouseDown events but retain the drawing methods of NSButtons super class.
My goal is as stated above, to subclass NSButton and to have it perform it's regular super class functionality whilst allowing me to override mouseDown and mouseUp and send it back to the action with the NSEvent so the button code can then examine what type of even occurred and respond respectively.
As I experimented with the NSButton sub class, I noticed that when you override the mouseDown as below:
override open func mouseDown(with event: NSEvent)
{
super.mouseDown(with: event)
// call the target with left button down
_ = target?.perform(action, with: event)
}
What appears to be happening is the the super.mouseDown captures the mouse events and subsequently your sub class of NSButton will not receive the appropriate mouseUp event.
Seeing as that is how things are working, I simply did an override of mouseDown and mouseUp without calling any of the Super Class functions. This indeed provides an even for mouseDown and mouseUp. I forward the event to the action and let the action code process which event has occurred and everything is fine. The only caveat is that the default behavior for the button state is not occurring. The button will stay in it's original non-selected state. If I change the state and force an update to the button, one would think it would draw the button in it's selected state. This does not happen hence why I am writing for some assistance.
I would love to be able to have the default drawing behavior of NSButton mouseDown event occur. Is there a way to set a property of NSButton aka it's state and force a redraw? I can't seem to be able to do this. If there is no way to do this, then I will be forced to draw the buttons content in it's selected state some how via overriding the draw method.
Any help would be appreciated.
You could inspect the mouseUp event in the #IBAction of the button instead:
#IBAction private func buttonClicked(_: AnyObject?) {
NSLog("Event: \(NSApp.currentEvent)")
}
If you don’t need the mouseDown event then there might be no need to subclass.
Note that the button can be activated with a keyboard, so you won’t receive mouse events, but you will receive the event from the action above.
using latest Chrome browser.
even after wiring up the onchange event for an inputsurface the dirty way since there is no extended event to just do inputsurface.on('change',function(){}):
MyInputSurface.prototype.deploy = function deploy(target) {
this._superDeploy(target);
target.onchange = function() {
console.log('test change');
};
target.onclick = function() {
console.log('test click');
};
};
The onchange event does not get fired until i click back in the browser window. The click event works just fine. Any advice?
The onchange event does not fire until the input loses focus. This is why you have to click in the browser window again before your callback is run.
From the IE Dev Center:
This event is fired when the contents are committed and not while the value is changing. For example, on a text box, this event is not fired while the user is typing, but rather when the user commits the change by leaving the text box that has focus. In addition, this event is executed before the code specified by onblur when the control is also losing the focus.
If you want the value of the input as the user is typing, you'll have to listen for the input's input event.
Again from IE Dev Center:
You can use the oninput to detect when the contents of a textArea, input type=text, or input type=password have changed. This event occurs immediately after modification, unlike the onchange event, which occurs when the element loses focus.
When your input callback fires, simply take the target value as you would with change.
I have noticed that when you drag the map, if you drag with an accelerating movement, after you release the mouse, the map has some sort of momentum and it keeps sliding for a short period of time, but the 'dragend' event is fired when you release the mouse (which to me it seems correct).
But how can you get the exact moment when the maps finishes SLIDING? I'm not interested in 'center_changed' / 'bounds_changed' solutions, because I need to make a XHR request on the event, and process some data.
How about the idle event, which is fired only once at the end (unlike center_changed and bounds_changed). From the docs:
This event is fired when the map becomes idle after panning or
zooming.
Try using idle event. Link to docs.
This event is fired when the map becomes idle after panning or
zooming.
If you want idle event to fire only after dragend then try the snippet below.
This code prints coordinates to console after both dragend and idle events fire.
mapObj.addListener('dragend', function () {
var idleListener = mapObj.addListener('idle', function () {
google.maps.event.removeListener(idleListener);
console.log(mapObj.getCenter().lat());
console.log(mapObj.getCenter().lng());
});
});
Im using Ext JS 4. I have a button inside a container. I want the container to receive mouseover, mouseout, and click events, but the button is causing the mouseover and mouseout event handlers to be called twice on the container. How can i disable the events on the child button?
I believe you are running to 4.0's new bubbleEvents behavior. Components now propagate some events to their parent containers. This happens in code, independent of DOM event bubbling, up the component hierarchy.
http://dev.sencha.com/deploy/ext-4.0.0/docs/api/Ext.container.Container.html
The best fix is to simply stop the event once you've handled it. That should prevent all bubbling, component or DOM. It is generally a good idea to do this for click events to make sure one and only one thing happens in response to a click, but I'm less certain it's appropriate for mouseover and mouseout.
panel.on('click', function(e) {
e.stopEvent();
// do your stuff
});
another fix you could try (I haven't) is to wipe bubbleEvents on the button.
Another, possibly less robust fix is to turn on event buffering:
el.on('click', this.onClick, this, {buffer: 10});
Buffering collapses duplicate events in a configurable time window into one and it's useful for smoothing out rapid fire or duplicate UI events, but I'm not sure how it plays with bubbling.
{
...
preventDefault: true,
...
}