Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.example;
- import java.awt.*;
- import java.awt.event.KeyEvent;
- import java.util.HashMap;
- import java.util.Map;
- public class Keyboard {
- private final Map<Integer, Boolean> pressedKeys = new HashMap<>();
- {
- KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(event -> {
- synchronized (Keyboard.class) {
- System.out.printf("%s, %s", event.getID(), event.getKeyCode());
- if (event.getID() == KeyEvent.KEY_PRESSED) pressedKeys.put(event.getKeyCode(), true);
- else if (event.getID() == KeyEvent.KEY_RELEASED) pressedKeys.put(event.getKeyCode(), false);
- return false;
- }
- });
- }
- public boolean isKeyPressed(int keyCode) { // Any key code from the KeyEvent class
- System.out.println(pressedKeys);
- return pressedKeys.get(keyCode) != null && pressedKeys.get(keyCode);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement