KeyBoard 操作 !

键盘操作:tab/ enter/ crtl+c ,crtl+v ;

 1 import java.awt.*;
 2 import java.awt.datatransfer.StringSelection;
 3 import java.awt.event.KeyEvent;
 4 
 5 /**
 6  * Created by ty on 2017/3/20.
 7  */
 8 public class KeyBoard {
 9 
10     //设定剪切板并进行字符串黏贴的封装方法
11     public static void setAndctrlVCipboardData(String string) {
12         //模拟Ctrl+V  进行黏贴操作
13         StringSelection stringSelection = new StringSelection(string);
14         Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
15         Robot robot = null;
16         try {
17             robot = new Robot();
18         } catch (AWTException el) {
19             el.printStackTrace();
20         }
21         robot.keyPress(KeyEvent.VK_CONTROL);
22         robot.keyPress(KeyEvent.VK_V);
23         robot.keyRelease(KeyEvent.VK_V);
24         robot.keyRelease(KeyEvent.VK_CONTROL);
25         try {
26             Thread.sleep(3000);
27         } catch (Exception e) {
28             e.printStackTrace();
29         }
30     }
31     //按tab的封装方法
32     public static void pressTabKey() {
33         Robot robot = null;
34         try {
35             robot = new Robot();
36         } catch (AWTException el) {
37             el.printStackTrace();
38         }
39         robot.keyPress(KeyEvent.VK_TAB);
40         robot.keyRelease(KeyEvent.VK_TAB);
41     }
42     //按Enter 键的封装
43     public static void PressEnter(){
44     Robot robot ;
45         try {
46             robot = new Robot();
47             robot.keyPress(KeyEvent.VK_ENTER);
48             robot.keyRelease(KeyEvent.VK_ENTER);
49         } catch (AWTException e) {
50             e.printStackTrace();
51         }
52 
53 
54     }
55 
56 
57 }

 

posted @ 2017-03-20 16:39  linbo.yang  阅读(351)  评论(0编辑  收藏  举报