Window向指定QQ客户端窗口发送消息

键盘上0~9的ASCII 

    public static final int VK_0              = 0x30;
    public static final int VK_1              = 0x31;
    public static final int VK_2              = 0x32;
    public static final int VK_3              = 0x33;
    public static final int VK_4              = 0x34;
    public static final int VK_5              = 0x35;
    public static final int VK_6              = 0x36;
    public static final int VK_7              = 0x37;
    public static final int VK_8              = 0x38;
    public static final int VK_9              = 0x39;

键盘上A~Z的ASCII  

    public static final int VK_A              = 0x41;
    public static final int VK_B              = 0x42;
    public static final int VK_C              = 0x43;
    public static final int VK_D              = 0x44;
    public static final int VK_E              = 0x45;
    public static final int VK_F              = 0x46;
    public static final int VK_G              = 0x47;
    public static final int VK_H              = 0x48;
    public static final int VK_I              = 0x49;
    public static final int VK_J              = 0x4A;
    public static final int VK_K              = 0x4B;
    public static final int VK_L              = 0x4C;
    public static final int VK_M              = 0x4D;
    public static final int VK_N              = 0x4E;
    public static final int VK_O              = 0x4F;
    public static final int VK_P              = 0x50;
    public static final int VK_Q              = 0x51;
    public static final int VK_R              = 0x52;
    public static final int VK_S              = 0x53;
    public static final int VK_T              = 0x54;
    public static final int VK_U              = 0x55;
    public static final int VK_V              = 0x56;
    public static final int VK_W              = 0x57;
    public static final int VK_X              = 0x58;
    public static final int VK_Y              = 0x59;
    public static final int VK_Z              = 0x5A;

 实现发送消息逻辑

public class SendQQMessage {

    public void sendQQMessage(Robot robot, Clipboard clipboard,String QQ, String message) throws Exception {

        //强行聊天的代码:tencent://Message/?Uin=919433667&websiteName=www.oicqzone.com&Menu=yes
        //强行加好友的代码:tencent://AddContact/fromId=45&fromSubId=1&subcmd=all&uin=574201314&fuin=919433667&website=www.oicqzone.com
        // String url = "http://wpa.qq.com/msgrd?v=3&uin=\""+QQ+"\"&site=qq&menu=yes";
        String url = "tencent://message/?uin=" + QQ + "&Site=null&Menu=yes";//弹出对应的QQ聊天窗口
        String cmd = "explorer \"" + url + "\"";
        Process process = null;
        try {
            process = Runtime.getRuntime().exec(cmd);
            // 等待程序执行结束并输出状态
            int i = process.waitFor();
            //延迟2秒等待聊天框开启
            //延迟1.5秒,主要是为了预留出打开窗口的时间,括号内的单位为毫秒
            //如果速度参数想要追求极限 50 
            robot.delay(1500);
            //默认从剪切板发送消息
            clipboard.setContents(new StringSelection(message), null);
            // 以下两行按下了ctrl+v,完成粘贴功能
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            // 释放ctrl按键,像ctrl,退格键,删除键这样的功能性按键,在按下后一定要释放,不然会出问题。crtl如果按住没有释放,
            // 在按其他字母按键是,敲出来的回事ctrl的快捷键。
            robot.keyRelease(KeyEvent.VK_CONTROL);
            // 延迟0.5秒再发送,这个时间可以自己改,想几秒发送一条都可以
            robot.delay(500);
            robot.keyPress(KeyEvent.VK_ENTER);// 回车
        } catch (IOException e) {
            System.out.println("请重新运行程序");
        } finally {
            assert process != null;
            process.getErrorStream().close();
            process.getInputStream().close();
            process.getOutputStream().close();
        }
    }
}

 

你可能感兴趣的:(赶时间的书生)