Package codedraw.events
Class EventScanner
java.lang.Object
codedraw.events.EventScanner
- All Implemented Interfaces:
AutoCloseable
The EventScanner works in the same way as a
The following example shows the current position of the mouse and the number of clicks:
The EventScanner automatically closes when the CodeDraw window is closed. The remaining events can still be consumed when the EventScanner is closed, but no new events will appear.
Scanner
.
The source of the events is specified as an argument to the constructor of the EventScanner.
If multi EventScanner listen to the same CodeDraw instance/window,
then the event will appear in both EventScanner.
The following example shows the current position of the mouse and the number of clicks:
CodeDraw cd = new CodeDraw();
EventScanner es = new EventScanner(cd);
int x = 0;
int y = 0;
int clickCount = 0;
while (!es.isClosed()) {
while (es.hasEventNow()) {
if (es.hasMouseMoveEvent()) {
MouseMoveEventArgs a = es.nextMouseMoveEvent();
x = a.getX();
y = a.getY();
}
if (es.hasMouseClickEvent()) {
clickCount++;
es.nextEvent();
}
else {
es.nextEvent();
}
}
cd.clear();
cd.drawText(100, 100, "Position: " + x + " " + y + "\nClick: " + clickCount);
cd.show();
}
This example first check if there are events available.
If there are, they get processed until there are no more events available.
Events that are not moves or clicks are ignored.
Once the events are processed the x/y position and clickCount are shown.
This continues in a loop until the user closes the window.
The EventScanner automatically closes when the CodeDraw window is closed. The remaining events can still be consumed when the EventScanner is closed, but no new events will appear.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the EventScanner.boolean
hasEvent()
Waits until the next event is available.boolean
Doesn't wait until the next event is available, but instead returns immediately.boolean
Waits until the next key down event is available.boolean
Waits until the next key press event is available.boolean
Waits until the next key up event is available.boolean
Waits until the next mouse click event is available.boolean
Waits until the next mouse down event is available.boolean
Waits until the next mouse enter event is available.boolean
Waits until the next mouse leave event is available.boolean
Waits until the next mouse move event is available.boolean
Waits until the next mouse up event is available.boolean
Waits until the next mouse wheel event is available.boolean
Waits until the next window close event is available.boolean
Waits until the next window move event is available.boolean
isClosed()
Checks whether this EventScanner is closed.Waits for the next event and then returns it.Waits for the next key down event and then consumes the event.Waits for the next key press event and then consumes the event.Waits for the next key up event and then consumes the event.Waits for the next mouse click event and then consumes the event.Waits for the next mouse down event and then consumes the event.Waits for the next mouse enter event and then consumes the event.Waits for the next mouse leave event and then consumes the event.Waits for the next mouse move event and then consumes the event.Waits for the next mouse up event and then consumes the event.Waits for the next mouse wheel event and then consumes the event.Waits for the next window close event and then consumes the event.Waits for the next window move event and then consumes the event.static <T> T
waitFor
(Function<EventHandler<T>, Subscription> codeDrawEvent) Waits until the next time the given event is triggered and then returns the event arguments as the result of this function.
-
Constructor Details
-
EventScanner
Creates a new EventScanner. Read the class documentation for a detailed explanation.- Parameters:
codeDraw
- the source of the events.
-
-
Method Details
-
waitFor
Waits until the next time the given event is triggered and then returns the event arguments as the result of this function. However, events that happen just before or after calling waitFor are lost. That means if events happen in between waitFor calls these events will be lost. To prevent that create an EventScanner object and use that object.
Example:MouseClickEventArgs args = EventScanner.waitFor(codeDraw::onMouseClick); // use args here
- Type Parameters:
T
- a type of event argument.- Parameters:
codeDrawEvent
- a CodeDraw Event.- Returns:
- The event argument.
-
hasEventNow
public boolean hasEventNow()Doesn't wait until the next event is available, but instead returns immediately. If there is currently an event available returns true otherwise false.- Returns:
- whether there are currently events available
-
hasEvent
public boolean hasEvent()Waits until the next event is available. Returns false if there are no more events.- Returns:
- whether there are more events available
-
hasMouseClickEvent
public boolean hasMouseClickEvent()Waits until the next mouse click event is available. Returns true if the next event is a mouse click event, otherwise false. The mouse click event is triggered once when a mouse button is pressed down and quickly released again.- Returns:
- whether the next event is a mouse click event.
-
hasMouseMoveEvent
public boolean hasMouseMoveEvent()Waits until the next mouse move event is available. Returns true if the next event is a mouse move event, otherwise false. The mouse move event is triggered continuously while the mouse is being moved.- Returns:
- whether the next event is a mouse move event.
-
hasMouseDownEvent
public boolean hasMouseDownEvent()Waits until the next mouse down event is available. Returns true if the next event is a mouse down event, otherwise false. The mouse down event is triggered exactly once when a mouse button is pressed down.- Returns:
- whether the next event is a mouse down event.
-
hasMouseUpEvent
public boolean hasMouseUpEvent()Waits until the next mouse up event is available. Returns true if the next event is a mouse up event, otherwise false. The mouse up event is triggered when a mouse button is released.- Returns:
- whether the next event is a mouse up event.
-
hasMouseEnterEvent
public boolean hasMouseEnterEvent()Waits until the next mouse enter event is available. Returns true if the next event is a mouse enter event, otherwise false. The mouse enter event is triggered when the mouse enters the canvas.- Returns:
- whether the next event is a mouse enter event.
-
hasMouseLeaveEvent
public boolean hasMouseLeaveEvent()Waits until the next mouse leave event is available. Returns true if the next event is a mouse leave event, otherwise false. The mouse leave event is triggered when the mouse leaves the canvas.- Returns:
- whether the next event is a mouse leave event.
-
hasMouseWheelEvent
public boolean hasMouseWheelEvent()Waits until the next mouse wheel event is available. Returns true if the next event is a mouse wheel event, otherwise false. The mouse wheel event is triggered each time the mouse wheel is turned.- Returns:
- whether the next event is a mouse wheel event.
-
hasKeyDownEvent
public boolean hasKeyDownEvent()Waits until the next key down event is available. Returns true if the next event is a key down event, otherwise false. The key down event is triggered exactly once when a key is pressed down.- Returns:
- whether the next event is a key down event.
-
hasKeyUpEvent
public boolean hasKeyUpEvent()Waits until the next key up event is available. Returns true if the next event is a key up event, otherwise false. The key up event is triggered exactly once when a key is released.- Returns:
- whether the next event is a key up event.
-
hasKeyPressEvent
public boolean hasKeyPressEvent()Waits until the next key press event is available. Returns true if the next event is a key press event, otherwise false. The key press event is triggered continuously while a key is being held down.- Returns:
- whether the next event is a key press event.
-
hasWindowMoveEvent
public boolean hasWindowMoveEvent()Waits until the next window move event is available. Returns true if the next event is a window move event, otherwise false. The window move event is triggered every time the CodeDraw window is moved.- Returns:
- whether the next event is a window move event.
-
hasWindowCloseEvent
public boolean hasWindowCloseEvent()Waits until the next window close event is available. Returns true if the next event is a window close event, otherwise false. The window close event is triggered exactly once when the user closes the window orCodeDraw.close()
is called.- Returns:
- whether the next event is a window close event.
-
nextEvent
Waits for the next event and then returns it. CheckhasEvent()
orhasEventNow()
before calling this function.
In the newer java version 17+ you can use switch pattern matching to handle events.EventScanner es = new EventScanner(codeDraw); while (es.hasEvent()) { switch (es.nextEvent()) { case MouseDownEventArgs a: System.out.println("The mouse has been pressed at x=" + a.getX() + " y=" + a.getY() + "."); break; case KeyDownEventArgs a: System.out.println("The " + a.getKey() + " has been pressed."); break; default: } }
- Returns:
- The event args as an object.
- Throws:
NoSuchElementException
- if there are no more events.
-
nextMouseClickEvent
Waits for the next mouse click event and then consumes the event. The mouse click event is triggered once when a mouse button is pressed down and quickly released again. CheckhasMouseClickEvent()
before calling this function.- Returns:
- a mouse click event.
- Throws:
InputMismatchException
- if the next event is not a mouse click event.NoSuchElementException
- if there are no more events.
-
nextMouseMoveEvent
Waits for the next mouse move event and then consumes the event. The mouse move event is triggered continuously while the mouse is being moved. CheckhasMouseMoveEvent()
before calling this function.- Returns:
- a mouse move event.
- Throws:
InputMismatchException
- if the next event is not a mouse move event.NoSuchElementException
- if there are no more events.
-
nextMouseDownEvent
Waits for the next mouse down event and then consumes the event. The mouse down event is triggered exactly once when a mouse button is pressed down. CheckhasMouseDownEvent()
before calling this function.- Returns:
- a mouse down event.
- Throws:
InputMismatchException
- if the next event is not a mouse down event.NoSuchElementException
- if there are no more events.
-
nextMouseUpEvent
Waits for the next mouse up event and then consumes the event. The mouse up event is triggered when a mouse button is released. CheckhasMouseUpEvent()
before calling this function.- Returns:
- a mouse up event.
- Throws:
InputMismatchException
- if the next event is not a mouse up event.NoSuchElementException
- if there are no more events.
-
nextMouseEnterEvent
Waits for the next mouse enter event and then consumes the event. The mouse enter event is triggered when the mouse enters the canvas. CheckhasMouseEnterEvent()
before calling this function.- Returns:
- a mouse enter event.
- Throws:
InputMismatchException
- if the next event is not a mouse enter event.NoSuchElementException
- if there are no more events.
-
nextMouseLeaveEvent
Waits for the next mouse leave event and then consumes the event. The mouse leave event is triggered when the mouse leaves the canvas. CheckhasMouseLeaveEvent()
before calling this function.- Returns:
- a mouse leave event.
- Throws:
InputMismatchException
- if the next event is not a mouse leave event.NoSuchElementException
- if there are no more events.
-
nextMouseWheelEvent
Waits for the next mouse wheel event and then consumes the event. The mouse wheel event is triggered each time the mouse wheel is turned. CheckhasMouseWheelEvent()
before calling this function.- Returns:
- a mouse wheel event.
- Throws:
InputMismatchException
- if the next event is not a mouse wheel event.NoSuchElementException
- if there are no more events.
-
nextKeyDownEvent
Waits for the next key down event and then consumes the event. The key down event is triggered exactly once when a key is pressed down. CheckhasKeyDownEvent()
before calling this function.- Returns:
- a key down event.
- Throws:
InputMismatchException
- if the next event is not a key down event.NoSuchElementException
- if there are no more events.
-
nextKeyUpEvent
Waits for the next key up event and then consumes the event. The key up event is triggered exactly once when a key is released. CheckhasKeyUpEvent()
before calling this function.- Returns:
- a key up event.
- Throws:
InputMismatchException
- if the next event is not a key up event.NoSuchElementException
- if there are no more events.
-
nextKeyPressEvent
Waits for the next key press event and then consumes the event. The key press event is triggered continuously while a key is being held down. CheckhasKeyPressEvent()
before calling this function.- Returns:
- a key press event.
- Throws:
InputMismatchException
- if the next event is not a key press event.NoSuchElementException
- if there are no more events.
-
nextWindowMoveEvent
Waits for the next window move event and then consumes the event. The window move event is triggered every time the CodeDraw window is moved. CheckhasWindowMoveEvent()
before calling this function.- Returns:
- a window move event.
- Throws:
InputMismatchException
- if the next event is not a window move event.NoSuchElementException
- if there are no more events.
-
nextWindowCloseEvent
Waits for the next window close event and then consumes the event. The window close event is triggered exactly once when the user closes the window orCodeDraw.close()
is called. CheckhasWindowCloseEvent()
before calling this function.- Returns:
- a window close event.
- Throws:
InputMismatchException
- if the next event is not a window close event.NoSuchElementException
- if there are no more events.
-
close
public void close()Closes the EventScanner. The EventScanner automatically closes when the CodeDraw window is closed. The remaining events can still be consumed when the EventScanner is closed, but no new events will appear.- Specified by:
close
in interfaceAutoCloseable
-
isClosed
public boolean isClosed()Checks whether this EventScanner is closed. The EventScanner automatically closes when the CodeDraw window is closed.- Returns:
- whether the EventScanner still received new events.
-