Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.blockhead7360.test.ppa;
- import java.awt.Dimension;
- import java.awt.event.ActionEvent;
- import javax.swing.AbstractAction;
- import javax.swing.JComponent;
- import javax.swing.JFrame;
- import javax.swing.JTextArea;
- import javax.swing.KeyStroke;
- public class TotalKeyTest {
- static JTextArea area;
- static int i = 0;
- public TotalKeyTest(){
- JFrame frame = new JFrame("Test");
- frame.setPreferredSize(new Dimension(600, 600));
- frame.setMaximumSize(new Dimension(600, 600));
- frame.setMinimumSize(new Dimension(600, 600));
- JComponent comp = (JComponent) frame.getContentPane();
- frame.getRootPane().getRootPane().setActionMap(bindKeys(comp).getActionMap());
- frame.getRootPane().getRootPane().setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, bindKeys(comp).getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW));
- frame.pack();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setResizable(false);
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- public static JComponent bindKeys(JComponent comp){
- comp.getInputMap().put(KeyStroke.getKeyStroke("pressed SPACE"), "pressSpace");
- comp.getActionMap().put("pressSpace", new AbstractAction(){
- private static final long serialVersionUID = 1L;
- public void actionPerformed(ActionEvent e){
- System.out.println("pressed Space");
- }
- });
- comp.getInputMap().put(KeyStroke.getKeyStroke("pressed A"), "pressLeft");
- comp.getActionMap().put("pressLeft", new AbstractAction(){
- private static final long serialVersionUID = 1L;
- public void actionPerformed(ActionEvent e){
- System.out.println("pressed left");
- }
- });
- comp.getInputMap().put(KeyStroke.getKeyStroke("released A"), "releaseLeft");
- comp.getActionMap().put("releaseLeft", new AbstractAction(){
- private static final long serialVersionUID = 1L;
- public void actionPerformed(ActionEvent e){
- System.out.println("released left");
- }
- });
- comp.getInputMap().put(KeyStroke.getKeyStroke("D"), "pressRight");
- comp.getActionMap().put("pressRight", new AbstractAction(){
- private static final long serialVersionUID = 1L;
- public void actionPerformed(ActionEvent e){
- System.out.println("pressed right");
- }
- });
- comp.getInputMap().put(KeyStroke.getKeyStroke("released D"), "releaseRight");
- comp.getActionMap().put("releaseRight", new AbstractAction(){
- private static final long serialVersionUID = 1L;
- public void actionPerformed(ActionEvent e){
- System.out.println("released right");
- }
- });
- return comp;
- }
- public static void main(String args[]){
- new TotalKeyTest();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement