Advertisement
kajacx

Java Robot class problem

Feb 5th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package test;
  6.  
  7. import java.awt.*;
  8. import java.awt.event.*;
  9. import javax.swing.JFrame;
  10.  
  11. /**
  12.  *
  13.  * @author kajacx
  14.  */
  15. public class RobotArrow {
  16.  
  17.     public static void main(String[] args) throws InterruptedException, AWTException {
  18.         JFrame f = new JFrame("TEST");
  19.         f.setSize(200, 200);
  20.         f.setLocationRelativeTo(null);
  21.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.         f.setVisible(true);
  23.  
  24.         KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
  25.         manager.addKeyEventDispatcher(new KeyEventDispatcher() {
  26.             @Override
  27.             public boolean dispatchKeyEvent(KeyEvent e) {
  28.                 if (e.getID() == KeyEvent.KEY_PRESSED) {
  29.                     System.out.format("code: %d, location: %d\n",
  30.                             e.getKeyCode(), e.getKeyLocation());
  31.                 }
  32.                 return false;
  33.             }
  34.         });
  35.        
  36.         Thread.sleep(1000);
  37.        
  38.         Robot bot = new Robot();
  39.        
  40.         bot.keyPress(KeyEvent.VK_UP);
  41.         bot.keyRelease(KeyEvent.VK_UP);
  42.        
  43.         bot.keyPress(KeyEvent.VK_KP_UP);
  44.         bot.keyRelease(KeyEvent.VK_KP_UP);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement