Skip to content

Commit

Permalink
Terminate application by CTRL+Q
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiri-Kaplan committed Jul 2, 2019
1 parent b9e059d commit ceebaf2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# FullscreenColors

A simple SWT fullscreen application running on primary monitor. When user hits any key application will draw random color.
A simple SWT fullscreen application running on primary monitor. When user hits any key application will draw random color. Application can be terminated by *CTRL+Q*
keys.

Requires Java10 and newer.

Expand Down
24 changes: 19 additions & 5 deletions src/com/github/jirkafm/FullscreenColorsControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

class FullscreenColorsControl implements Listener {
class FullscreenColorsControl {

private final Random random;
private final Shell shell;
Expand All @@ -19,16 +18,31 @@ public FullscreenColorsControl(Shell shell) {
}

public void init() {
shell.getDisplay().addFilter(SWT.KeyDown, this);
shell.getDisplay().addFilter(SWT.KeyDown, this::generateColor);
shell.getDisplay().addFilter(SWT.KeyDown, this::exitSequenceCheck);
}

@Override
public void handleEvent(Event event) {
public void generateColor(Event event) {
final int keyCode = event.keyCode;
final int red = keyCode % 255;
final int green = random.nextInt(255);
final int blue = random.nextInt(255);
shell.setBackground(new Color(shell.getDisplay(), red, green, blue));
}

private void exitSequenceCheck(Event event) {
if (isExitKeySequence(event)) {
shell.close();
}
}

private boolean isExitKeySequence(Event event) {
final int keyCode = event.keyCode;
return isCtrlHold(event) && 'q' == keyCode;
}

private boolean isCtrlHold(Event event) {
return SWT.CTRL == (event.stateMask & SWT.CTRL);
}

}

0 comments on commit ceebaf2

Please sign in to comment.