SWT: How do you register KeyUp events when no Control have focus? - events

Im am making an Java SWT program that is required to run on both Linux and Windows.
I use the following Code to listen for KeyUp events:
Control.addListener(SWT.KeyUp, new Listener() {
public void handleEvent(Event arg0) {
System.out.println("Event");
}
});
But this does not trigger when no control has focus.
Do anyone know of a place i can add a listener that acts as a Catch-all?

The only way of doing this that I'm aware of is by placing a filter on the Display. Take note that multiple Shells may operate on one Display, so you should be careful!
shell.getDisplay().addFilter(SWT.KeyDown, new Listener() {
public void handleEvent(final Event event) {
System.out.println(event);
}
});

try following method in Display class:
public void addListener ( int eventType, Listener listener )

I have not been able to find a solution to this. I suspect none exist

Related

How to use CXCallObserver in Xamarin?

I need to subscribe an event to handle incoming phone call. Since iOS version 11.0 CTCallCenter is deprecated we have to use CXCallObserver. I successfully implemented solution for CTCallCenter, but I am not able to subscribe event for CXCallObserver. Does anyone have working solution for CXCallObserver?
Here is my code to subscribe event for CTCallCenter..
_callCenter = new CTCallCenter();
_callCenter.CallEventHandler += CallEvent;
private void CallEvent(CTCall call)
{
CoreFoundation.DispatchQueue.MainQueue.DispatchSync(() =>
{
if(call.CallState.Equals(call.StateIncoming))
//Do something
});
}
Implement the delegate for CXCallObserver:
public class MyCXCallObserverDelegate : CXCallObserverDelegate
{
public override void CallChanged(CXCallObserver callObserver, CXCall call)
{
Console.WriteLine(call);
}
}
Then in your code, create a instance of CXCallObserver (maintain a strong reference to this) and then assign the delegate:
cXCallObserver = new CXCallObserver();
cXCallObserver.SetDelegate(new MyCXCallObserverDelegate(), null);

JavaFX Repeater

I want to have something like a TimerTask in JavaFX.
I have a order of Functions, this Functions should be repeated every 1/2 Second maybe every 1/4 Second.
This Functions have some effects for a GUI Component in JavaFX.
Can you give me an TimerTask (JavaFX) example ? I can not use Timer Task, becouse the Compiler said this:
Exception in thread "Timer-0" java.lang.IllegalStateException: Not on FX application thread; currentThread = Timer-0
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:237)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:398)
at javafx.scene.Parent$1.onProposedChange(Parent.java:245)
at com.sun.javafx.collections.VetoableObservableList.clear(VetoableObservableList.java:146)
at com.sun.javafx.charts.Legend$1.onChanged(Legend.java:55)
at com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(ListListenerHelper.java:134)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:48)
at com.sun.javafx.collections.ObservableListWrapper.callObservers(ObservableListWrapper.java:97)
at com.sun.javafx.collections.ObservableListWrapper.clear(ObservableListWrapper.java:184)
at javafx.scene.chart.AreaChart.updateLegend(AreaChart.java:420)
at javafx.scene.chart.XYChart$2.onChanged(XYChart.java:96)
at com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(ListListenerHelper.java:134)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:48)
at com.sun.javafx.collections.ObservableListWrapper.callObservers(ObservableListWrapper.java:97)
at com.sun.javafx.collections.ObservableListWrapper.removeFromList(ObservableListWrapper.java:383)
at com.sun.javafx.collections.ObservableListWrapper.removeAll(ObservableListWrapper.java:271)
at de.sick.suit.framework.control.fx.HistogramChart.deleteData(HistogramChart.java:170)
at de.sick.suit.framework.samples.ImageHistogram.run(ImageHistogram.java:200)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
Thank you for your help!
You can use a ScheduledService. The example from the javadoc:
you may want to ping a server on a regular basis to see if there are any updates. Such as ScheduledService might be implemented like this:
ScheduledService<Document> svc = new ScheduledService<>(Duration.seconds(1)) {
protected Task<Document> createTask() {
return new Task<Document>() {
protected Document call() {
// Connect to a Server
// Get the XML document
// Parse it into a document
return document;
}
}
// TIMER
Timeline line = new Timeline(new KeyFrame(Duration.seconds(0.2), new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent arg0)
{
sek++;
puls(imgHisto, startingArea);
System.out.println(" Sek: " + sek);
}
}));
line.setCycleCount(Animation.INDEFINITE);
line.play();
I handeld the Problem with a TimeLine. This works very fine for my example!
You need to provide more Code, but as of now I think you are not updating the UI properly. If you are in another Thread (which you seem to be if you are using a Task), whenever you want to update the UI you need to use
Platform.runLater();
Your Stacktrace indicates this.
You were updating GUI component outside the JavaFX application thread.
Try something like this
Platform.runLater(new Runnable(){
#Override
public void run(){
//update GUI here
}
});

JavaFX blocking native key event handling for treeview

I would like to change handling of key pressed events for example to do something else when user press up/down arrows but when i add eventHandler by setOnKeyPressed/setOnKeyReleased i cant stop native handling of those keys.
Example:
treeView.setOnKeyPressed(new EventHandler<KeyEvent>() {
private KeyCodeCombination prevNodeKeyCombination = new KeyCodeCombination(KeyCode.UP);
private KeyCodeCombination nextNodeKeyCombination = new KeyCodeCombination(KeyCode.DOWN);
public void handle(KeyEvent event)
{
if (prevNodeKeyCombination.match(event))
{
selectPrevSibling();
}
else if (nextNodeKeyCombination.match(event))
{
selectNextSibling();
}
event.consume(); // i try to block anything that goes by
}
});
Any idea how to override native key handling, if necessary i can try extend TreeView if that helps in something?
To override native handling of event use addEventFilter in my case it was:
treeView.addEventFilter(KeyEvent.ANY, new EventHandler<KeyEvent>() { ... }

GWT capture event in RichTextArea or in IFrame

I have a RichTextArea
private RichTextArea richTextArea;
and I'm trying to capture a paste event like this:
DOM.sinkEvents((com.google.gwt.user.client.Element) richTextArea.getElement(), com.google.gwt.user.client.Event.ONPASTE);
DOM.setEventListener((com.google.gwt.user.client.Element) richTextArea.getElement(), new EventListener(){
#Override public void onBrowserEvent(Event event) {
switch (event.getTypeInt()) {
case Event.ONPASTE: Window.alert("hey");break;
}
}
});
But it doesn't work, when I paste text on the richTextArea the alert is not triggered.
Any idea how to capture this paste event?
Thanks!
You cannot add the event to the RichTextArea, which actually is an iframe, but to it's body.
Although you could use jsni, I would use gwtquery because its simplicity:
// First attach the widget to the DOM
RootPanel.get().add(richTextArea);
// We only can bind events to the content, once the iframe document has been created,
// and this happens after it has been attached. Note that richtTextArea uses a timeout
// to initialize, so we have to delay the event binding as well
$(richTextArea).delay(1, lazy().contents().find("body").bind(Event.ONPASTE, new Function(){
#Override public boolean f(Event e) {
Window.alert("OnPaste");
return true;
}
}).done());
Have you seen the rendered HTML of RichTextArea ? it's an iframe not an actual textarea input type. it sets the user input under a body element. So that's why you don't get sinked onpaste events. For example if you listen to onpaste on TextArea widget it works fine.
private static class MyTextArea extends TextArea
{
public MyTextArea()
{
sinkEvents(Event.ONPASTE);
}
#Override
public void onBrowserEvent(Event event)
{
if(event.getTypeInt() == Event.ONPASTE)
{
Window.alert("text pasted !");
}
super.onBrowserEvent(event);
}
}
maybe you can bind a handler to that iframe body element using JSNI and get the callback on that event (haven't tried it though )
Just for the sake of completeness, the native (JSNI) solution would be something like:
setPastehandler(richTextArea.getElement());
private native void setPasteHandler(Element e) /*-{
e.contentDocument.onpaste = function (event) {
alert("pasted!");
};
}-*/;

javafx - how to disable events fired not from the user

i have a little problem with javafx. i added a change listener like this:
private final ChangeListener<String> pageItemSelected = new ChangeListener<String>()
{
#Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue){
pageGotSelected(newValue);
}
};
now to the problem: if i change an page item like this:
guiPageList.setValue(model.getCurrentTargetPage());
the event gets also(as it get by selecting something with the mouse or key) fired. is there a way to disable the event firing or another way?
i need the event only, if the element got selected by the user and not if i change it with the setValue() function...
perhaps consuming the event, but i donĀ“t know what kind of event this would be.
thanks in advance!!!
You can temporarily remove the listener and add it again:
guiPageList.getSelectionModel().selectedItemProperty().removeListener(pageItemSelected);
guiPageList.setValue(model.getCurrentTargetPage());
guiPageList.getSelectionModel().selectedItemProperty().addListener(pageItemSelected);
Alternatively you could decorate the listener with another listener implementation, the code would be something like:
class InvalidationListenerEventBlocker implements InvalidationListener {
InvalidationListener decoratedListener;
boolean block;
public void invalidated(Observable observable) {
if(!block) {
decoratedListener.invalidated(observable);
}
}
}
Add a setter for the block boolean and send the listener in through the constructor. Set block to true to stop events.
This is very old question, but I came to some solution I personally use, that's reusable and does not require storing a reference to the listener (but it needs a reference to the exposing/muffling property thou).
So first the concept: we're going to create lambda (InvalidationListener), that is going to be called only if above mentioned exposing/muffling property is set to true/false. For that we are going to define another functional interface that provides described behavior:
#FunctionalInterface
private interface ManageableInvalidationListener
extends InvalidationListener {
public static InvalidationListener exposing(
BooleanProperty expose,
ManageableInvalidationListener listener) {
return ob -> {
if (expose.get()) {
listener.invalidate(ob);
}
};
}
public static InvalidationListener muffling(
BooleanProperty muffle,
ManageableInvalidationListener listener) {
return ob -> {
if (!muffle.get()) {
listener.invalidated(ob);
}
}
}
public abstract void invalidated(Observable ob);
}
This interface defines two static methods we're going to use in our code. We pass a steering property as first argument (it will tell if listener should be called) and actual implementation to be performed, when it will be called. Please note, that there is no need to extend InvalidationListener, but I'd like to keep ManageableInvalidationListener in sync with InvalidationListener.
So we would call exposing if we need to create a (manageabale) listener that would notify the (invalidation) listener if expose property has value of true. In other case we would create the listener with muffling, if true of the steering property would mean, well, to muffle the notification.
How to use it?
//- Let's make life easier and import expose method statically
import static ManageableInvalidationListener.exposing;
// ...
//- This is the steering property.
BooleanProperty notify = new SimpleBooleanProperty(true);
//- This is our main property with the listener.
ObjectProperty<Foobar> foobar = new SimpleObjectProperty<>();
//- Let's say we are going to notify the listener, if the
// notify property is set to true.
foobar.addListener(exposing(notify, ob -> {
//- Here comes the InvalidListener code.
}));
And then somewhere in the code:
//- Listener will be notified as usual.
foobar.set(new Foobar());
//- Now temporarily disable notifications.
notify.set(false);
//- The listener will not get the notification this time.
foobar.set(new Foobar());
//- Re-enable notifications.
notify.set(true);
Hope this somehow helps. You're free to use the code in this post as pleases you.

Resources