Created
April 15, 2019 16:30
-
-
Save uvlad7/689082d06468de067e4b2d8c5efdbd4e to your computer and use it in GitHub Desktop.
УП 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.geometry.HPos; | |
import javafx.geometry.VPos; | |
import javafx.scene.Scene; | |
import javafx.scene.image.Image; | |
import javafx.scene.layout.ColumnConstraints; | |
import javafx.scene.layout.RowConstraints; | |
import javafx.scene.text.Font; | |
import javafx.stage.Stage; | |
import java.awt.*; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
/** | |
* A sample that demonstrates various key events and their usage. Type in the | |
* text box to view the triggered events: key pressed, key typed and key | |
* released. Pressing the Shift, Ctrl, and Alt keys also trigger events. | |
* | |
* @see javafx.scene.input.KeyCode | |
* @see javafx.scene.input.KeyEvent | |
* @see javafx.event.EventHandler | |
*/ | |
public class Main extends Application { | |
public static void main(String[] args) { | |
launch(args); | |
} | |
private void init(Stage stage) { | |
MyPane root = new MyPane(); | |
root.setPrefWidth(800); | |
root.setPrefHeight(400); | |
MyListView console = new MyListView(); | |
MyLabel lastKey = new MyLabel(); | |
lastKey.setFocusTraversable(true); | |
lastKey.setFont(new Font("BodoniCameoC", 72)); | |
root.addObserver(console); | |
root.addObserver(lastKey); | |
root.setOnKeyPressed(root::notifyObservers); | |
ColumnConstraints columnConstraints1 = new ColumnConstraints(); | |
columnConstraints1.setPercentWidth(80); | |
columnConstraints1.setHalignment(HPos.CENTER); | |
ColumnConstraints columnConstraints2 = new ColumnConstraints(); | |
columnConstraints2.setPercentWidth(20); | |
RowConstraints rowConstraints = new RowConstraints(); | |
rowConstraints.setValignment(VPos.CENTER); | |
rowConstraints.setPercentHeight(100); | |
root.getColumnConstraints().addAll(columnConstraints1, columnConstraints2); | |
root.getRowConstraints().addAll(rowConstraints); | |
root.add(lastKey, 0, 0); | |
root.add(console, 1, 0); | |
Scene scene = new Scene(root); | |
stage.setScene(scene); | |
stage.setTitle("Key Logger"); | |
try (FileInputStream icon = new FileInputStream("src\\logger.png")) { | |
stage.getIcons().add(new Image(icon)); | |
} catch (IOException e) { | |
} | |
stage.sizeToScene(); | |
} | |
@Override | |
public void start(Stage primaryStage) { | |
init(primaryStage); | |
primaryStage.show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.scene.control.Label; | |
import javafx.scene.input.KeyEvent; | |
public class MyLabel extends Label implements Observer { | |
@Override | |
public void handle(KeyEvent k) { | |
String text = MyUtils.getText(k).toUpperCase(); | |
setText(text); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.scene.control.ListView; | |
import javafx.scene.input.KeyEvent; | |
public class MyListView extends ListView<String> implements Observer { | |
public MyListView() { | |
this.setFocusTraversable(false); | |
} | |
@Override | |
public void handle(KeyEvent k) { | |
String text = MyUtils.getText(k); | |
getItems().add("Key Pressed: " + text); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.scene.input.KeyEvent; | |
import javafx.scene.layout.GridPane; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MyPane extends GridPane implements Observable { | |
private List<Observer> observers; | |
public MyPane() { | |
this.observers = new ArrayList<>(); | |
} | |
@Override | |
public void addObserver(Observer o) { | |
observers.add(o); | |
} | |
@Override | |
public void notifyObservers(KeyEvent k) { | |
observers.forEach(o -> o.handle(k)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.scene.input.KeyCode; | |
import javafx.scene.input.KeyEvent; | |
public class MyUtils { | |
public static String getText(KeyEvent ke) { | |
KeyCode code = ke.getCode(); | |
if (code.equals(KeyCode.UNDEFINED)) { | |
switch (ke.getText()) { | |
case "Ж": | |
case "ж": { | |
code = KeyCode.SEMICOLON; | |
break; | |
} | |
case "Э": | |
case "э": { | |
code = KeyCode.QUOTE; | |
break; | |
} | |
case "Ё": | |
case "ё": { | |
code = KeyCode.BACK_QUOTE; | |
break; | |
} | |
case "Б": | |
case "б": { | |
code = KeyCode.COMMA; | |
break; | |
} | |
case "Ю": | |
case "ю": { | |
code = KeyCode.PERIOD; | |
break; | |
} | |
} | |
} | |
return code.getName(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.scene.input.KeyEvent; | |
public interface Observable { | |
void addObserver(Observer o); | |
void notifyObservers(KeyEvent k); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.scene.input.KeyEvent; | |
public interface Observer { | |
void handle(KeyEvent k); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment