Beispiel #1
0
        /// <summary>
        /// 添加命令按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addProgram_Click(object sender, EventArgs e)
        {
            String command;

            command = Command.Text.Substring(Command.Text.LastIndexOf('+') + 1);
            LoadConfig writeConfig;
            String     rule = @"[A-Z0-9,./;'\[\\=]|]|-";
            Match      m    = Regex.Match(command, rule);

            if (!m.Success)
            {
                MessageBox.Show("Space+后面的字符必须为A-Z0-9,./;'[]\\-=字符中的一个");
                return;
            }
            //将;[=进行特殊处理
            switch (command)
            {
            case ";":
                command = "sm";
                break;

            case "[":
                command = "lb";
                break;

            case "=":
                command = "eq";
                break;

            default:
                break;
            }
            switch (resourceType.SelectedIndex)
            {
            case 0:
                String fileName = program.FileName;
                if (command.Trim() == "")
                {
                    hi.setHint("请输入指令!");
                    hi.Show();
                    return;
                }
                if (fileName == "")
                {
                    hi.setHint("请选择程序!");
                    hi.Show();
                    return;
                }
                writeConfig = new LoadConfig(Application.StartupPath + @"\command.ini");
                if (args.Text.Trim() != "")
                {
                    fileName += "?" + args.Text.Trim();
                }
                if (resource.Text != "选择文件" && resource.Text.Trim() != "")
                {
                    if (args.Text.Trim() == "")
                    {
                        fileName += "?" + resource.Text.Trim();
                    }
                    else
                    {
                        fileName += " " + resource.Text.Trim();
                    }
                }
                if (writeConfig.ReadIni("Command List", command) != "")
                {
                    DialogResult dr = MessageBox.Show(command + "热键已存在,是否覆盖?", "咒语记混了吗?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (dr == DialogResult.OK)
                    {
                        writeConfig.IniWriteValue("Command List", command.Trim(), fileName);
                    }
                    else if (dr == DialogResult.Cancel)
                    {
                    }
                }
                else
                {
                    writeConfig.IniWriteValue("Command List", command.Trim(), fileName);
                }
                hi.Close();
                Close();
                break;

            case 1:
                String folderName = "explorer.exe?" + folder.Text;
                if (command.Trim() == "")
                {
                    hi.setHint("请输入指令!");
                    hi.Show();
                    return;
                }
                if (folder.Text.Trim() == "")
                {
                    hi.setHint("请选择文件夹!");
                    hi.Show();
                    return;
                }
                writeConfig = new LoadConfig(Application.StartupPath + @"\command.ini");
                if (writeConfig.ReadIni("Command List", command) != "")
                {
                    DialogResult dr = MessageBox.Show(command + "热键已存在,是否覆盖?", "咒语记混了吗?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (dr == DialogResult.OK)
                    {
                        writeConfig.IniWriteValue("Command List", command.Trim(), folderName);
                    }
                    else if (dr == DialogResult.Cancel)
                    {
                    }
                }
                else
                {
                    writeConfig.IniWriteValue("Command List", command.Trim(), folderName);
                }
                hi.Close();
                Close();
                break;

            case 2:
                String url = webSite.Text;
                if (url.Trim() == "")
                {
                    hi.setHint("请输入URL!");
                    hi.Show();
                    return;
                }
                if (!url.Contains(":"))
                {
                    MessageBox.Show(url + "URL格式错误!请加上协议头。");
                    return;
                }
                String site = "explorer.exe?" + url;
                if (command.Trim() == "")
                {
                    hi.setHint("请输入指令!");
                    hi.Show();
                    return;
                }

                writeConfig = new LoadConfig(Application.StartupPath + @"\command.ini");
                if (writeConfig.ReadIni("Command List", command) != "")
                {
                    DialogResult dr = MessageBox.Show(command + "热键已存在,是否覆盖?", "咒语记混了吗?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (dr == DialogResult.OK)
                    {
                        writeConfig.IniWriteValue("Command List", command.Trim(), site);
                    }
                    else if (dr == DialogResult.Cancel)
                    {
                    }
                }
                else
                {
                    writeConfig.IniWriteValue("Command List", command.Trim(), site);
                }
                hi.Close();
                Close();
                break;

            default:
                MessageBox.Show("请选择类型!");
                break;
            }
        }
        /// <summary>
        /// 客户端键盘捕捉事件
        /// </summary>
        /// <param name="hookStruct">由Hook程序发送的按键信息</param>
        /// <param name="handle">是否拦截</param>
        public void KeyPress(KeyBoardHook.HookStruct hookStruct, out bool handle)
        {
            handle = false;
            //全屏程序中禁用
            if (RunningFullScreenApp)
            {
                return;
            }
            //判断是否在打字
            if (hookStruct.vkCode != (int)Keys.Space && flag == false)
            {
                istyping = true;
                t.Start();
                delay.Stop();
            }
            if (istyping == false && (flag == true || (hookStruct.vkCode == (int)(Keys.Space) && hookStruct.flags == 0)))
            {
                if (hookStruct.vkCode == (int)Keys.Space && hookStruct.flags == 0)
                {
                    delay.Enabled = true;
                    delay.Start();
                }
                if (hookStruct.vkCode != (int)Keys.Space && hookStruct.flags == 0)
                {
                    String comtxt = null;
                    if (keycode.ContainsKey(hookStruct.vkCode))
                    {
                        comtxt = keycode[hookStruct.vkCode];
                    }
                    else
                    {
                        comtxt = ((char)hookStruct.vkCode).ToString();
                    }
                    String fileName = ini.ReadIni("Command List", comtxt);

                    if (fileName != "")
                    {
                        keybd_event(8, 0, 0, 0);
                        loading.AnimateImage();
                        loading.Show();
                        lt.Start();
                        handle = true;  //拦截键盘字符
                        String[] progargs = fileName.Split('?');
                        String   program  = progargs[0];

                        if (progargs.Length < 2)
                        {
                            if (System.IO.File.Exists(program))
                            {
                                try
                                {
                                    System.Diagnostics.Process.Start(program);
                                }
                                catch (Exception pse)
                                {
                                    MessageBox.Show(pse.Message);
                                }
                            }
                            else
                            {
                                hi.setHint("文件“" + program + "”不存在!");
                                hi.Show();
                            }
                        }
                        else
                        {
                            if (System.IO.Directory.Exists(progargs[1].Trim()) || progargs[1].Contains(":"))
                            {
                                try
                                {
                                    System.Diagnostics.Process.Start(program, progargs[1].Trim());
                                }
                                catch (Exception pse)
                                {
                                    MessageBox.Show(pse.Message);
                                }
                            }
                            else
                            {
                                hi.setHint("路径" + progargs[1].Trim() + "不存在");
                                hi.Show();
                            }
                        }
                    }
                    else
                    {
                        return;
                    }
                }
            }
            //空格键抬起
            if (hookStruct.vkCode == (int)(Keys.Space) && hookStruct.flags == 128)
            {
                handle = false;
                delay.Stop();
                flag = false;
            }
        }